Skip to content

Instantly share code, notes, and snippets.

@CasonBarnhill
Created October 21, 2015 03:57
Show Gist options
  • Save CasonBarnhill/b9986b1b1bfacc2aeea6 to your computer and use it in GitHub Desktop.
Save CasonBarnhill/b9986b1b1bfacc2aeea6 to your computer and use it in GitHub Desktop.
Normal: *Requirements 1. Game is played on a 3 x 3 board where players take turns putting 'X' and 'O' in an empty square. 2. A player wins by having all X's or all O's in vertically/horizantly/diagonally. 3. The board is drawn to the console each time a position is played.
Hard: Have the player play as the 'X' and the computer play as an 'O'
Nightmare: The computer plays intelligently (or at least as well as one would expect :) )
class Program
{
private static object currentPlayer;
static void DrawBoard(string[] box)
{
Console.Clear();
Console.WriteLine($"{box[0]} | {box[1]} | {box[2]}");
Console.WriteLine($"{box[3]} | {box[4]} | {box[5]}");
Console.WriteLine($"{box[6]} | {box[7]} | {box[8]}");
}
static void Main(string[] mybox)
{
string[] board = { "1", "2", "3",
"4", "5", "6",
"7", "8", "9" };
string player = "O";
Console.WriteLine("Get Ready For Some Tic Tac Toe!!!");
DrawBoard(board);
//draw the board
DrawBoard(board);
while (true)
{
//ask for a move from the current player]
int position = AskForMove(player);
//check if move is valid
// bool valid = AskForMove(position);
//if not then ask again
//updated the board at the slot they specified.
board[position - 1] = player;
//check for the 8 possible win conditions.
bool winner = winGame(board, player);
//if they won, print they won
if (winner == true)
{
Console.WriteLine($"Player{player} Won!!!");
break;
}
//if the board is full, then print "tie game"
bool noRoomOnBoard = maximumCapacity(board);
if (noRoomOnBoard)
{
Console.WriteLine($"Tie ;(");
break;
}
//otherwise swap player turns
player = swapPlayer(player);
}
Console.ReadLine();
}
private static string swapPlayer(string player)
{
if (player == "O")
{
return "X";
}
else
{
return "O";
}
}
private static int AskForMove(string player)
{
Console.WriteLine($"Your move player {player}");
string move = Console.ReadLine();
var moveAsNumber = int.Parse(move);
return moveAsNumber;
}
private static bool maximumCapacity(string[] board)
{
foreach (var square in board)
{
//if not an x or o then there's a number and still playable
if (square != "X" && square != "O") ;
}
{
return (false);
}
}
private static bool winGame(string[] board, string player)
{
if (board[0] == player && board[4] == player && board[8] == player)
{
return true;
}
if (board[2] == player && board[4] == player && board[6] == player)
{
return true;
}
if (board[0] == player && board[1] == player && board[2] == player)
{
return true;
}
if (board[3] == player && board[4] == player && board[5] == player)
{
return true;
}
if (board[6] == player && board[7] == player && board[8] == player)
{
return true;
}
if (board[0] == player && board[3] == player && board[6] == player)
{
return true;
}
if (board[1] == player && board[4] == player && board[7] == player)
{
return true;
}
if (board[2] == player && board[5] == player && board[8] == player)
{
return true;
}
if (board[0] == player && board[4] == player && board[8] == player)
{
return true;
}
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment