Skip to content

Instantly share code, notes, and snippets.

@brunotdantas
Created October 23, 2022 19:19
Show Gist options
  • Save brunotdantas/d31ee9b84afe7b7e891c0e91458e06fb to your computer and use it in GitHub Desktop.
Save brunotdantas/d31ee9b84afe7b7e891c0e91458e06fb to your computer and use it in GitHub Desktop.
tictactoe_csharp
using System;
internal class Program
{
private static void Main(string[] args)
{
//-------- CODE HERE
Console.Title = "The Point";
Console.Clear();
Console.WriteLine("---------------------------------------------------------------------------------------------------------------");
Console.WriteLine("This is TIC-TAC-TOE, 2 players will play until one of them get 3 sequence of his Symbol ( X or O ) or draw after 9 rounds");
Console.WriteLine("We will play this game using the NUMERIC Keyboard as references to the squares, ");
Console.WriteLine("in other words the numbers you should input will be the same as your numeric keyboard, like this:");
Console.WriteLine("[7] | [8] | [9]");
Console.WriteLine("[4] | [5] | [6]");
Console.WriteLine("[1] | [2] | [3]");
Console.WriteLine("---------------------------------------------------------------------------------------------------------------");
Console.WriteLine("When you are ready press Enter to start");
Console.ReadKey();
Console.Clear();
TicTacToeGame game = new TicTacToeGame();
game.Run();
ExitProgram();
}
static void ExitProgram()
{
Console.WriteLine("");
Console.WriteLine(":::::::::::::::::::::::::");
Console.WriteLine("Thanks for playing!");
Console.WriteLine(":::::::::::::::::::::::::");
Environment.Exit(0);
}
}
public class TicTacToeGame
{
int currentPlayer = 1;
public void Run()
{
// Generate Board
Board board = new Board();
// Player 1
Player player1 = new Player(1);
// Player 2
Player player2 = new Player(2);
int currentTurn = 0;
// Get move
while (currentTurn < 9)
{
board.getValidMove(currentPlayer);
//checkEnd
if (board.EndGame(currentPlayer))
{
Console.WriteLine($"*****************************************************");
Console.WriteLine($"Player {currentPlayer} you are the WINNER!");
Console.WriteLine($"*****************************************************");
break;
}
// change player turn
currentPlayer = currentPlayer == 1 ? 2 : 1;
currentTurn++;
}
if (currentTurn == 9)
{
Console.WriteLine($"*****************************************************");
Console.WriteLine($"We have a draw!");
Console.WriteLine($"*****************************************************");
}
board.ShowBoard();
}
}
public class Board
{
int[,] board = new int[3, 3];
public void getValidMove(int player)
{
bool validChoice = true;
string playerChoice = "";
int value;
do
{
ShowBoard();
Console.WriteLine($"Player {player} what is your move?");
playerChoice = Console.ReadLine();
if (int.TryParse(playerChoice, out value))
{
if (makeMove(value, player))
validChoice = false;
}
else
Console.WriteLine("\nYou should type a NUMBER between 1 and 9");
} while (validChoice);
Console.Clear();
}
public bool makeMove(int position, int player)
{
if (position > 9 || position < 1)
{
Console.WriteLine(">> Invalid position, try again");
return false;
}
/* Using numeric keyboard as reference the positions
7 | 8 | 9
4 | 5 | 6
1 | 2 | 3
*/
// As the board is filled with int, 1 = "X" player and 2 = "O" Player
if (position == 7 && board[0, 0] == 0)
{
board[0, 0] = player;
return true;
}
if (position == 8 && board[0, 1] == 0)
{
board[0, 1] = player;
return true;
}
if (position == 9 && board[0, 2] == 0)
{
board[0, 2] = player;
return true;
}
// --- new row
if (position == 4 && board[1, 0] == 0)
{
board[1, 0] = player;
return true;
}
if (position == 5 && board[1, 1] == 0)
{
board[1, 1] = player;
return true;
}
if (position == 6 && board[1, 2] == 0)
{
board[1, 2] = player;
return true;
}
// --- new row
if (position == 1 && board[2, 0] == 0)
{
board[2, 0] = player;
return true;
}
if (position == 2 && board[2, 1] == 0)
{
board[2, 1] = player;
return true;
}
if (position == 3 && board[2, 2] == 0)
{
board[2, 2] = player;
return true;
}
Console.WriteLine($">> Position {position} not available, try again");
return false;
}
public void ShowBoard()
{
Console.WriteLine("----------------------------------------------------------------------------");
Console.WriteLine("Board current state:");
Console.Write($"[{board[0, 0].ToString().Replace("1", "X").Replace("2", "O").Replace("0", " ")}]|");
Console.Write($"[{board[0, 1].ToString().Replace("1", "X").Replace("2", "O").Replace("0", " ")}]|");
Console.Write($"[{board[0, 2].ToString().Replace("1", "X").Replace("2", "O").Replace("0", " ")}] \n");
Console.Write($"[{board[1, 0].ToString().Replace("1", "X").Replace("2", "O").Replace("0", " ")}]|");
Console.Write($"[{board[1, 1].ToString().Replace("1", "X").Replace("2", "O").Replace("0", " ")}]|");
Console.Write($"[{board[1, 2].ToString().Replace("1", "X").Replace("2", "O").Replace("0", " ")}] \n");
Console.Write($"[{board[2, 0].ToString().Replace("1", "X").Replace("2", "O").Replace("0", " ")}]|");
Console.Write($"[{board[2, 1].ToString().Replace("1", "X").Replace("2", "O").Replace("0", " ")}]|");
Console.Write($"[{board[2, 2].ToString().Replace("1", "X").Replace("2", "O").Replace("0", " ")}] \n");
Console.WriteLine("----------------------------------------------------------------------------");
}
public bool EndGame(int player)
{
// check horizontal
if (board[0, 0] == player && board[0, 1] == player && board[0, 2] == player) return true;
if (board[1, 0] == player && board[1, 1] == player && board[1, 2] == player) return true;
if (board[2, 0] == player && board[2, 1] == player && board[2, 2] == player) return true;
// check vertical
if (board[0, 0] == player && board[1, 0] == player && board[2, 0] == player) return true;
if (board[0, 1] == player && board[1, 1] == player && board[2, 1] == player) return true;
if (board[0, 2] == player && board[1, 2] == player && board[2, 2] == player) return true;
// check diagonals
if (board[2, 0] == player && board[1, 1] == player && board[0, 2] == player) return true;
if (board[0, 0] == player && board[1, 1] == player && board[2, 2] == player) return true;
return false;
}
}
public class Player
{
public int playerNumber { get; private set; }
public Player(int plNumber)
{
this.playerNumber = playerNumber;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment