C# 텍스트 틱택토 전체 코드
using System;
namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
int boardX = 3;
int boardY = 3;
TicTacToe ticTacToe = new TicTacToe(boardX, boardY);
TicTacToe.BoardState turn = TicTacToe.BoardState.Player1;
while (true)
{
ticTacToe.PrintBoard();
Console.Write("Select Num(1~9) : ");
int selectIndex = int.Parse(Console.ReadLine()) - 1;
if (!ticTacToe.SetBoardState(turn, selectIndex))
{
Console.WriteLine("You choose wrong number");
}
else if(ticTacToe.IsWinOnIndex(selectIndex))
{
string playerName = turn == TicTacToe.BoardState.Player1 ? "Player1" : "Player2";
Console.Write($"{playerName} is win");
break;
}
else if(turn == TicTacToe.BoardState.Player1)
{
turn = TicTacToe.BoardState.Player2;
}
else
{
turn = TicTacToe.BoardState.Player1;
}
}
}
}
class TicTacToe
{
public enum BoardState
{
None = 0,
Player1,
Player2
}
BoardState[,] board;
public TicTacToe(int x, int y)
{
board = new BoardState[x, y];
}
public void PrintBoard()
{
int x = board.GetLength(0);
int y = board.GetLength(1);
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
if (board[j, i] == BoardState.Player1)
{
Console.Write("o ");
}
else if (board[j, i] == BoardState.Player2)
{
Console.Write("x ");
}
else
{
Console.Write("_ ");
}
}
Console.WriteLine();
}
}
public bool SetBoardState(BoardState state, int index)
{
int x = index % board.GetLength(0);
int y = index / board.GetLength(0);
if (board[x, y] != BoardState.None)
{
return false;
}
board[x, y] = state;
return true;
}
int GetCountOfSameStateByDirection(int currentX, int currentY, int directionX, int directionY)
{
if ((currentX == 0 && directionX == -1) ||
(currentY == 0 && directionY == -1) ||
(currentX == board.GetLength(0)-1 && directionX == 1) ||
(currentY == board.GetLength(1)-1 && directionY == 1))
{
return 0;
}
int nextStateX = currentX + directionX;
int nextStateY = currentY + directionY;
if (board[currentX, currentY] == board[nextStateX, nextStateY])
{
return 1 + GetCountOfSameStateByDirection(nextStateX, nextStateY, directionX, directionY);
}
else
{
return GetCountOfSameStateByDirection(nextStateX, nextStateY, directionX, directionY);
}
}
public bool IsWinOnIndex(int index)
{
int boardX = board.GetLength(0);
int boardY = board.GetLength(1);
int x = index % boardX;
int y = index / boardX;
if (GetCountOfSameStateByDirection(x, y, -1, 0) + GetCountOfSameStateByDirection(x, y, 1, 0) + 1 == boardX ||
GetCountOfSameStateByDirection(x, y, 0, -1) + GetCountOfSameStateByDirection(x, y, 0, 1) + 1 == boardY)
{ // check horizontal and vertical
return true;
}
//check diagonal 1->9
int sameStateCount = GetCountOfSameStateByDirection(x, y, -1, -1) + GetCountOfSameStateByDirection(x, y, 1, 1) + 1;
if(sameStateCount == boardX || sameStateCount == boardY)
{
return true;
}
//check diagonal 7->3
sameStateCount = GetCountOfSameStateByDirection(x, y, -1, 1) + GetCountOfSameStateByDirection(x, y, 1, -1) + 1;
if (sameStateCount == boardX || sameStateCount == boardY)
{
return true;
}
return false;
}
}
}
'활동 > 내일배움캠프 Unity' 카테고리의 다른 글
[내배캠] TIL 텍스트 RPG 과제 답지 코드 분석 및 개선 (1) | 2024.09.23 |
---|---|
[내배캠] TIL 권장하는 TIL 작성법 + 스네이크 게임 과제 코드 + 블랙잭 과제 코드 (0) | 2024.09.20 |
[내배캠] TIL 미니프로젝트 발표 (1) | 2024.09.13 |
[내배캠] TIL 카드 뒤집기 게임 미니 프로젝트 완료 (0) | 2024.09.12 |
[내배캠] TIL : 유니티 라이프사이클 (0) | 2024.09.11 |