틱택토 게임 구현
using System;
bool gameStart = true;
const int TicTacToe = 9;
const int ColumCount = 3;
int[] board = new int[TicTacToe];
bool[] matched = new bool[TicTacToe];
int player = 1;
int count = 0;
while (gameStart)
{
Title();
DisPlayBoard(board, matched);
string choiceName = player == 1 ? "X" : "O";
int index = ReadBoardIndex(choiceName, matched);
board[index] = player;
matched[index] = true;
count++;
// 가로 3줄이 동일시 승리
if (board[0] == player && board[1] == player && board[2] == player ||
board[3] == player && board[4] == player && board[5] == player ||
board[6] == player && board[7] == player && board[8] == player)
{
DisPlayBoard(board, matched);
Console.WriteLine($"플레이어 {choiceName} 승리!");
break;
}
// 세로 3줄이 동일시 승리
if (board[0] == player && board[3] == player && board[6] == player ||
board[1] == player && board[4] == player && board[7] == player ||
board[2] == player && board[5] == player && board[8] == player)
{
DisPlayBoard(board, matched);
Console.WriteLine($"플레이어 {choiceName} 승리!");
break;
}
// 대각선 3줄이 동일시 승리
if (board[0] == player && board[4] == player && board[8] == player ||
board[2] == player && board[4] == player && board[6] == player)
{
DisPlayBoard(board, matched);
Console.WriteLine($"플레이어 {choiceName} 승리!");
break;
}
if (count == TicTacToe)
{
DisPlayBoard(board, matched);
Console.WriteLine("무승부입니다.");
break;
}
if (player == 1)
{
player = 2;
}
else
{
player = 1;
}
}
WaitForEnter();
void WaitForEnter()
{
Console.WriteLine("게임을 종료하시려면 Enter를 눌러주세요.");
Console.ReadLine();
}
int ReadBoardIndex(string choiceName, bool[] matched)
{
while (true)
{
Console.WriteLine($"플레이어 {choiceName}의 차례입니다.");
Console.Write("위치를 선택하세요 (1~9): ");
if (!int.TryParse(Console.ReadLine(), out int input))
{
Console.WriteLine("숫자를 입력하세요.");
continue;
}
if (input < 1 || input > 9)
{
Console.WriteLine("1부터 9까지 입력하세요.");
continue;
}
if (matched[input - 1] == true)
{
Console.WriteLine("이미 선택된 위치입니다.");
continue;
}
else
{
return input - 1;
}
}
}
void DisPlayBoard(int[] board, bool[] matched)
{
Console.Clear();
Title();
Console.WriteLine();
Console.WriteLine("위치 번호:");
for (int i = 0; i < board.Length; i += ColumCount)
{
Console.WriteLine(string.Join(" | ",
i + 1, i + 2, i + 3));
if (i + ColumCount < board.Length)
{
Console.WriteLine("---------");
}
}
Console.WriteLine();
Console.WriteLine("현재 보드:");
string[] shows = new string[board.Length];
for (int i = 0; i < board.Length; i++)
{
string show = ".";
if (matched[i] == true)
{
if (board[i] == 1)
{
show = "X";
}
else
{
show = "O";
}
}
shows[i] = show;
}
for (int i = 0; i < shows.Length; i += ColumCount)
{
Console.WriteLine(string.Join(" | ",
shows[i], shows[i + 1], shows[i + 2]));
if (i + ColumCount < shows.Length)
{
Console.WriteLine("---------");
}
}
Console.WriteLine();
}
void Title()
{
Console.WriteLine("=== 틱택토 게임 1단계 ===");
Console.WriteLine("두 사람이 X와 O를 번갈아 놓습니다.");
Console.WriteLine("X가 먼저 시작합니다.");
}
오늘은 선생님께서 내주신 틱택토 게임 과제를 만들어 보았다.
이번 게임을 제작하는 것도 꽤 애를 먹었다.
승리 조건을 구현하는 방법, 숫자 사이에 " | " 를 넣는 방법, 번갈아 가면서 칸을 입력하는 것 등
강의 자료를 찾아보지 않았다면 쉽게 해결 하지 못했을 것이다.
처음부터 끝까지 내 힘으로 작성하고 싶었지만 생각보다 복잡하여 과거 만들었던 카드 게임을 참고했다.
더 성장하여 무언가를 참고하지 않고 머릿속으로 코드를 짤 수 있으면 좋겠다.
분명 더 쉽고 예쁘게 코드를 작성할 수 있는 방법이 있을 거 같은데 지금은 생각나지 않는다.




부럽습니다... 하나도 구현 못하고있는데..ㅠㅠ
제 코드가 영 좋은 코드는 아닌거 같긴한데 참고하셔도 좋을거같아요!