Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 109 additions & 16 deletions TicTacToe/TicTacToe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@ class Program
new string[] {" ", " ", " "},
new string[] {" ", " ", " "},
new string[] {" ", " ", " "}

};

public static void Main()
{
do
{
/*swap player turn ? Does this make sence to execute*/

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic is slightly off, you draw the board before and after each player plays.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better comments on the methods would have been nice.

DrawBoard();
GetInput();

/*loop */
} while (!CheckForWin() && !CheckForTie());

// leave this command at the end so your program does not close automatically
Expand All @@ -29,51 +32,141 @@ public static void GetInput()
{
Console.WriteLine("Player " + playerTurn);
Console.WriteLine("Enter Row:");
int row = int.Parse(Console.ReadLine());

string input = Console.ReadLine();
int row = 0;
// limits input to 0, 1, 2
if(input == "0" || input == "1" || input == "2")
{
row = int.Parse(input);
}
else
{
Console.WriteLine("Invalid Input, Try Again");
GetInput();
}


Console.WriteLine("Enter Column:");
int column = int.Parse(Console.ReadLine());
string inputtwo = Console.ReadLine();
int column = 0;
if(inputtwo == "0" || inputtwo == "1" || inputtwo == "2")
{
column = int.Parse(inputtwo);
}
else
{
Console.WriteLine("Invalid Input, Try Again");
GetInput();
}

PlaceMark(row,column);
if (playerTurn == "O")
{
playerTurn = "X";
}
else
{
playerTurn ="O";
}

DrawBoard();
}

/*update board with correct value and position */
public static void PlaceMark(int row, int column)
{
board[row][column] = playerTurn;
// your code goes here


}

public static bool CheckForWin()
{
// your code goes here
bool won = false;
if(HorizontalWin() || VerticalWin() || DiagonalWin())
{
won = true;
Console.Write("You Won!");
}

return false;
return won;
}

public static bool CheckForTie()
{
// your code goes here

return false;
// checking for Tie
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not really checking for a tie, its just checking that every position is played.

bool tie = false;
if(board [0][0] != " " && board [0][1] != " " && board [0][2] != " " && board [1][0] != " " && board [1][1] != " " && board [1][2] != " " && board [2][0] != " " && board [2][1] != " " && board [2][2] != " ")
{
tie = true;
Console.Write("Tie!");
}
return tie;
}
/*this method will return true when any row has three of a kind in said row otherwise it will return false */

public static bool HorizontalWin()
{
// your code goes here
/* if cells are empty what happens, make sure empty does not count as win */

bool WonHorizontally = false;
if(board [0][0] == board[0][1] && board [0][1] == board [0][2] && board [0][0] != " " && board [0][1] != " " && board [0][2] != " ")
{
//Console.Write("Horizon one");
WonHorizontally = true;
}
else if(board [1][0] == board[1][1] && board [1][1] == board [1][2] && board [1][0] != " " && board [1][1] != " " && board [1][2] != " ")
{
//Console.Write("Horizon two");
WonHorizontally = true;
}
else if(board [2][0] == board[2][1] && board [2][1] == board [2][2] && board [2][0] != " " && board [2][1] != " " && board [2][2] != " ")
{
//Console.Write("Horizon three");
WonHorizontally = true;
}
//Console.Write("Horizontal Won");
return WonHorizontally;

return false;
}

public static bool VerticalWin()
{
// your code goes here

return false;
bool WonVertically = false;
if(board [0][0] == board[1][0] && board [1][0] == board [2][0] && board [0][0] != " " && board [1][0] != " " && board [2][0] != " ")
{
WonVertically = true;
}
else if(board [0][1] == board[1][1] && board [1][1] == board [2][1] && board [0][1] != " " && board [1][1] != " " && board [2][1] != " ")
{
WonVertically = true;
}
else if(board [0][2] == board[1][2] && board [1][2] == board [2][2] && board [0][2] != " " && board [1][2] != " " && board [2][2] != " ")
{
WonVertically = true;
}
//0Console.Write("Vertical Won");
return WonVertically;
}

public static bool DiagonalWin()
{
// your code goes here

return false;
bool WonDiagonally = false;
if(board [0][0] == board[1][1] && board [1][1] == board [2][2] && board [0][0] != " " && board [1][1] != " " && board [2][2] != " ")
{
WonDiagonally = true;
}
else if(board [0][2] == board[1][1] && board [1][1] == board [2][0] && board [0][2] != " " && board [1][1] != " " && board [0][2] != " ")
{
WonDiagonally = true;
}
//Console.Write("Diagonal Won");
return WonDiagonally;
}


public static void DrawBoard()
{
Console.WriteLine(" 0 1 2");
Expand Down