Created
October 21, 2015 14:06
-
-
Save CasonBarnhill/7503c94d8093fab43ac7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program | |
{ | |
private static object player; | |
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