diff --git a/TicTacToe/TicTacToe.cs b/TicTacToe/TicTacToe.cs index c5751176..b429464d 100644 --- a/TicTacToe/TicTacToe.cs +++ b/TicTacToe/TicTacToe.cs @@ -1,15 +1,17 @@ -using System; + +using System; namespace TicTacToe { class Program { + //Starting turn. public static string playerTurn = "X"; - public static string[][] board = new string[][] - { - new string[] {" ", " ", " "}, - new string[] {" ", " ", " "}, - new string[] {" ", " ", " "} + //board array is used to create the board and hold the data for the game. + public static string[][] board = new string[][] { + new string[] { " ", " ", " " }, + new string[] { " ", " ", " " }, + new string[] { " ", " ", " " } }; public static void Main() @@ -18,13 +20,31 @@ public static void Main() { DrawBoard(); GetInput(); - } while (!CheckForWin() && !CheckForTie()); - - // leave this command at the end so your program does not close automatically + if (CheckForWin() == true) + { + DrawBoard(); + Console.WriteLine("{0} LOSES.!", playerTurn); + } + else if (CheckForTie()) + { + DrawBoard(); + Console.WriteLine("You tied!"); + } Console.ReadLine(); - } + bool test = true; + if (!test) + { + test = testXO(); + Console.WriteLine("Your test is {0}", test); + } + else + { + Console.WriteLine("Your test is {0}", test); + } + } + //getInput fx takes user input and uses it to fill board and call PlaceMark public static void GetInput() { Console.WriteLine("Player " + playerTurn); @@ -32,56 +52,189 @@ public static void GetInput() int row = int.Parse(Console.ReadLine()); Console.WriteLine("Enter Column:"); int column = int.Parse(Console.ReadLine()); + PlaceMark(row, column); + playerTurn = (playerTurn == "X") ? "O" : "X"; } - public static void PlaceMark(int row, int column) + public static string PlaceMark(int row, int column) { - // your code goes here + if (board[row][column] == " ") + { + board[row][column] = playerTurn; + return playerTurn; + } + else if (board[row][column] == "X" || board[row][column] == "O") + { + Console.WriteLine("There's something here, Chief."); + GetInput(); + //Changes Turn. + playerTurn = (playerTurn == "X") ? "O" : "X"; + } + return playerTurn; } - + //checks all win conditions for a win. public static bool CheckForWin() { - // your code goes here - - return false; + if (HorizontalWin() == true) + { + return true; + } + else if (VerticalWin() == true) + { + return true; + } + else if (DiagonalWin() == true) + { + return true; + } + else + { + return false; + } } - + //looks at the number of spaces - players keep going until you win or tie. public static bool CheckForTie() { // your code goes here - - return false; + int spaceAvail = 9; + for (int i = 0; i < board.Length; i++) + { + for (int j = 0; j < board[i].Length; j++) + { + if (board[i][j] == "X" || board[i][j] == "O") + { + spaceAvail--; + } + } + } + if (spaceAvail <= 0 && !CheckForWin() == true) + { + return true; + } + else + { + return false; + } } - + public static bool HorizontalWin() { - // your code goes here - - return false; + if (board[0][0] == "X" && board[0][1] == "X" && board[0][2] == "X") + { + return true; + } + else if (board[0][0] == "O" && board[0][1] == "O" && board[0][2] == "O") + { + return true; + } + else if (board[1][0] == "X" && board[1][1] == "X" && board[1][2] == "X") + { + return true; + } + else if (board[1][0] == "O" && board[1][1] == "O" && board[1][2] == "O") + { + return true; + } + else if (board[2][0] == "X" && board[2][1] == "X" && board[2][2] == "X") + { + return true; + } + else if (board[2][0] == "O" && board[2][1] == "O" && board[2][2] == "O") + { + return true; + } + else + { + return false; + } } public static bool VerticalWin() { - // your code goes here - - return false; + //Checks each column for a veritical win. + if (board[0][0] == "X" && board[1][0] == "X" && board[2][0] == "X") + { + return true; + } + else if (board[0][0] == "O" && board[1][0] == "O" && board[2][0] == "O") + { + return true; + } + else if (board[0][1] == "X" && board[1][1] == "X" && board[2][1] == "X") + { + return true; + } + else if (board[0][1] == "O" && board[1][1] == "O" && board[2][1] == "O") + { + return true; + } + else if (board[0][2] == "X" && board[1][2] == "X" && board[2][2] == "X") + { + return true; + } + else if (board[0][2] == "O" && board[1][2] == "O" && board[2][2] == "O") + { + return true; + } + else + { + return false; + } } public static bool DiagonalWin() { - // your code goes here - - return false; + if (board[0][0] == "X" && board[1][1] == "X" && board[2][2] == "X") + { + return true; + } + else if (board[0][0] == "O" && board[1][1] == "O" && board[2][2] == "O") + { + return true; + } + else if (board[0][2] == "O" && board[1][1] == "O" && board[2][0] == "O") + { + return true; + } + else if (board[0][2] == "O" && board[1][1] == "O" && board[2][0] == "O") + { + return true; + } + else + { + return false; + } } public static void DrawBoard() { - Console.WriteLine(" 0 1 2"); + Console.WriteLine(" 0 1 2 "); Console.WriteLine("0 " + String.Join("|", board[0])); - Console.WriteLine(" -----"); + Console.WriteLine(" ------"); Console.WriteLine("1 " + String.Join("|", board[1])); - Console.WriteLine(" -----"); + Console.WriteLine(" ------"); Console.WriteLine("2 " + String.Join("|", board[2])); } + + public static bool testXO() + { + PlaceMark(0, 0); + PlaceMark(1, 0); + PlaceMark(0, 1); + PlaceMark(1, 1); + PlaceMark(0, 2); + if (CheckForWin() == true) + { + if (HorizontalWin() == true) + { + return true; + } + } + else + { + return false; + } + return false; + } } -} +} \ No newline at end of file diff --git a/Week3Quiz/Program.cs b/Week3Quiz/Program.cs new file mode 100644 index 00000000..1ca1ef4c --- /dev/null +++ b/Week3Quiz/Program.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; + +namespace Week3Quiz +{ + class Program + { + static void Main(string[] args){ + // ARRAYS + // String[] names = new String[4]; + + // names[0] = "John"; + // names[1] = "Mike"; + // names[2] = "Adam"; + // names[3] = "Jane"; + + // String[] pets = {"Rocky", "Walter", "Bailey"}; + + // String[] cities; + // cities = new String[] {"Austin", "Dallas", "Houston"}; + + // Console.WriteLine("Names {0}, {1}, {2}, {3}.", names); + + // int sizeOfArray = names.Length; + // int lastIndex = sizeOfArray-1; + // String lastName - names[lastIndex]; + + // Console.WriteLine("In the array of size {0}, {1} is at index {2}.", sizeOfArray, lastIndex, lastName); + + List names = new List(); + names.Add("John"); + names.Add("Mike"); + names.Add("Mark"); + + Console.WriteLine("This list has {0} elements", names.Count) + } + + } +} diff --git a/Week3Quiz/Week3Quiz.csproj b/Week3Quiz/Week3Quiz.csproj new file mode 100644 index 00000000..23df6047 --- /dev/null +++ b/Week3Quiz/Week3Quiz.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp2.1 + + +