From fb31233286fd0ab29427b4494c5ba96569761b4e Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 19 Jan 2019 00:08:51 -0600 Subject: [PATCH 01/19] 100% working going to cleaan up the code from this point --- TicTacToe/TicTacToe.cs | 172 +++++++++++++++++++++++++++++------------ 1 file changed, 121 insertions(+), 51 deletions(-) diff --git a/TicTacToe/TicTacToe.cs b/TicTacToe/TicTacToe.cs index c5751176..5d24999c 100644 --- a/TicTacToe/TicTacToe.cs +++ b/TicTacToe/TicTacToe.cs @@ -4,84 +4,154 @@ namespace TicTacToe { class Program { - public static string playerTurn = "X"; - public static string[][] board = new string[][] - { - new string[] {" ", " ", " "}, - new string[] {" ", " ", " "}, - new string[] {" ", " ", " "} - }; - public static void Main() { - do + bool win = false; + while (!win) { - DrawBoard(); - GetInput(); - - } while (!CheckForWin() && !CheckForTie()); + string playerTurn = "X"; + int turn = 0; + string[,] board = new string[3, 3]; + + for (int i = 1; i <= 9; i++) + { + printBoard(board); + Console.WriteLine("Player " + playerTurn); + Console.WriteLine("Enter Row:"); + int row = Int32.Parse(Console.ReadLine()); + Console.WriteLine("Enter Column:"); + int column = Int32.Parse(Console.ReadLine()); + Console.Clear(); + //check if that spot is taken + if (isTaken(board, row, column)) + { + Console.WriteLine("Sorry that spot is taken"); + } + else + { + board[row, column] = playerTurn; + playerTurn = (playerTurn == "X") ? "O" : "X"; + } + + if ((2 % i) == 0) + { + turn++; + } + + string winner = "n"; + if (i >= 3) + { + Console.WriteLine("checking winner"); + winner = checkWin(board); + } + + if (i >= 7) + { + winner = checkTie(board); + } + + if (winner == "X" || winner == "O") + { + win = true; + break; + } + } + + return; + } - // leave this command at the end so your program does not close automatically - Console.ReadLine(); } - public static void GetInput() + public static bool isTaken(string[,] board, int row, int column) { - Console.WriteLine("Player " + playerTurn); - Console.WriteLine("Enter Row:"); - int row = int.Parse(Console.ReadLine()); - Console.WriteLine("Enter Column:"); - int column = int.Parse(Console.ReadLine()); + if (String.IsNullOrEmpty(board[row, column])) + { + return false; + } + else + { + return true; + } } - public static void PlaceMark(int row, int column) + public static string checkWin(string[,] board) { - // your code goes here + string checkRows = checkHorizontal(board); + string checkColumns = checkVertical(board); + string checkCorner2Corner = checkDiagonal(board); + if (checkRows == "X" || checkColumns == "X" || checkCorner2Corner == "X") + { + return "X"; + } + else if (checkRows == "O" || checkColumns == "O" || checkCorner2Corner == "O") + { + return "O"; + } + else + { + return "n"; + } } - - public static bool CheckForWin() + public static string checkHorizontal(string[,] board) { - // your code goes here + string isWinner = "n"; + for (int i = 0; i <= 2; i++) + { + if (board[i, 0] == board[i, 1] && board[i, 0] == board[i, 2] && !String.IsNullOrEmpty(board[i, 0])) + { + isWinner = board[i, 0]; + } + } + Console.WriteLine(isWinner); + return isWinner; - return false; } - public static bool CheckForTie() + public static string checkVertical(string[,] board) { - // your code goes here + string isWinner = "n"; + for (int i = 0; i <= 2; i++) + { + if (board[0, i] == board[1, i] && board[0, i] == board[2, i] && !String.IsNullOrEmpty(board[0, i])) + { + isWinner = board[0, i]; + } + } + return isWinner; - return false; } - - public static bool HorizontalWin() - { - // your code goes here - return false; + public static string checkDiagonal(string[,] board) + { + string isWinner = "n"; + if (board[0, 0] == board[1, 1] && board[0, 0] == board[2, 2] && !String.IsNullOrEmpty(board[0, 0])) + { + isWinner = board[0, 0]; + } + else if (board[0, 2] == board[1, 1] && board[0, 0] == board[2, 0] && !String.IsNullOrEmpty(board[0, 2])) + { + isWinner = board[0, 2]; + } + return isWinner; } - public static bool VerticalWin() + public static string checkTie(string[,] board) { - // your code goes here - - return false; + return "n"; } - public static bool DiagonalWin() + public static void showBoard(string[,] board) { - // your code goes here - - return false; + return; } - - public static void DrawBoard() + public static void printBoard(string[,] board) { - Console.WriteLine(" 0 1 2"); - Console.WriteLine("0 " + String.Join("|", board[0])); - Console.WriteLine(" -----"); - Console.WriteLine("1 " + String.Join("|", board[1])); - Console.WriteLine(" -----"); - Console.WriteLine("2 " + String.Join("|", board[2])); + Console.WriteLine(" " + "0 " + " 1 " + " 2"); + Console.WriteLine("0 " + board[0, 0] + "| " + board[0, 1] + " |" + board[0, 2]); + Console.WriteLine(" " + "--------"); + Console.WriteLine("1 " + board[1, 0] + "| " + board[1, 1] + " |" + board[1, 2]); + Console.WriteLine(" " + "--------"); + Console.WriteLine("2 " + board[2, 0] + "| " + board[2, 1] + " |" + board[2, 2]); } } } From 4d645f08d21c2f36fc6f674fa7f846a490f010bd Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 19 Jan 2019 00:53:55 -0600 Subject: [PATCH 02/19] finished tic tac toe, going to write it recursive and hopefully test all the win conditions --- TicTacToe/.vscode/launch.json | 26 ++++++++++++++++++++++++++ TicTacToe/.vscode/tasks.json | 15 +++++++++++++++ TicTacToe/TicTacToe.cs | 12 ++++++------ keepScoreTicTT/Program.cs | 12 ++++++++++++ keepScoreTicTT/keepScoreTicTT.csproj | 8 ++++++++ 5 files changed, 67 insertions(+), 6 deletions(-) create mode 100644 TicTacToe/.vscode/launch.json create mode 100644 TicTacToe/.vscode/tasks.json create mode 100644 keepScoreTicTT/Program.cs create mode 100644 keepScoreTicTT/keepScoreTicTT.csproj diff --git a/TicTacToe/.vscode/launch.json b/TicTacToe/.vscode/launch.json new file mode 100644 index 00000000..7e5b81d4 --- /dev/null +++ b/TicTacToe/.vscode/launch.json @@ -0,0 +1,26 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": ".NET Core Launch (console)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + "program": "${workspaceFolder}/bin/Debug/netcoreapp2.0/TicTacToe.dll", + "args": [], + "cwd": "${workspaceFolder}", + "console": "integratedTerminal", + "stopAtEntry": false, + "internalConsoleOptions": "openOnSessionStart" + }, + { + "name": ".NET Core Attach", + "type": "coreclr", + "request": "attach", + "processId": "${command:pickProcess}" + } + ] +} \ No newline at end of file diff --git a/TicTacToe/.vscode/tasks.json b/TicTacToe/.vscode/tasks.json new file mode 100644 index 00000000..a6ea07f7 --- /dev/null +++ b/TicTacToe/.vscode/tasks.json @@ -0,0 +1,15 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/TicTacToe.csproj" + ], + "problemMatcher": "$msCompile" + } + ] +} \ No newline at end of file diff --git a/TicTacToe/TicTacToe.cs b/TicTacToe/TicTacToe.cs index 5d24999c..57890958 100644 --- a/TicTacToe/TicTacToe.cs +++ b/TicTacToe/TicTacToe.cs @@ -146,12 +146,12 @@ public static void showBoard(string[,] board) } public static void printBoard(string[,] board) { - Console.WriteLine(" " + "0 " + " 1 " + " 2"); - Console.WriteLine("0 " + board[0, 0] + "| " + board[0, 1] + " |" + board[0, 2]); - Console.WriteLine(" " + "--------"); - Console.WriteLine("1 " + board[1, 0] + "| " + board[1, 1] + " |" + board[1, 2]); - Console.WriteLine(" " + "--------"); - Console.WriteLine("2 " + board[2, 0] + "| " + board[2, 1] + " |" + board[2, 2]); + Console.WriteLine(" " + "0" + " 1 " + "2"); + Console.WriteLine("0 " + board[0, 0] + "|" + board[0, 1] + "|" + board[0, 2]); + Console.WriteLine(" " + "-----"); + Console.WriteLine("1 " + board[1, 0] + "|" + board[1, 1] + "|" + board[1, 2]); + Console.WriteLine(" " + "-----"); + Console.WriteLine("2 " + board[2, 0] + "|" + board[2, 1] + "|" + board[2, 2]); } } } diff --git a/keepScoreTicTT/Program.cs b/keepScoreTicTT/Program.cs new file mode 100644 index 00000000..f5da485f --- /dev/null +++ b/keepScoreTicTT/Program.cs @@ -0,0 +1,12 @@ +using System; + +namespace keepScoreTicTT +{ + class Program + { + static void Main(string[] args) + { + Console.WriteLine("Hello World!"); + } + } +} diff --git a/keepScoreTicTT/keepScoreTicTT.csproj b/keepScoreTicTT/keepScoreTicTT.csproj new file mode 100644 index 00000000..21dff5ca --- /dev/null +++ b/keepScoreTicTT/keepScoreTicTT.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp2.2 + + + From 1e25549c7349708ee08ae2b48a4c324d2e04c94a Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 19 Jan 2019 13:07:01 -0600 Subject: [PATCH 03/19] making recursive and test --- TicTacToe/TicTacToe.cs | 2 +- keepScoreTicTT/Program.cs | 174 +++++++++++++++++++++++++++++++++++++- 2 files changed, 172 insertions(+), 4 deletions(-) diff --git a/TicTacToe/TicTacToe.cs b/TicTacToe/TicTacToe.cs index 57890958..a1bfe6e0 100644 --- a/TicTacToe/TicTacToe.cs +++ b/TicTacToe/TicTacToe.cs @@ -128,7 +128,7 @@ public static string checkDiagonal(string[,] board) { isWinner = board[0, 0]; } - else if (board[0, 2] == board[1, 1] && board[0, 0] == board[2, 0] && !String.IsNullOrEmpty(board[0, 2])) + else if (board[0, 2] == board[1, 1] && board[0, 2] == board[2, 0] && !String.IsNullOrEmpty(board[0, 2])) { isWinner = board[0, 2]; } diff --git a/keepScoreTicTT/Program.cs b/keepScoreTicTT/Program.cs index f5da485f..56f947f2 100644 --- a/keepScoreTicTT/Program.cs +++ b/keepScoreTicTT/Program.cs @@ -1,12 +1,180 @@ using System; -namespace keepScoreTicTT +namespace TicTacToe { class Program { - static void Main(string[] args) + public static void Main() { - Console.WriteLine("Hello World!"); + playGame(); + + } + + public static void playGame() + { + bool win = false; //If the win is true, game is over + while (!win) + { + string playerTurn = "X"; + int turn = 0; + string[,] board = new string[3, 3]; + + for (int i = 1; i <= 9; i++) + { + printBoard(board); //Make the Board + Console.WriteLine("Player " + playerTurn); + Console.WriteLine("Enter Row:"); //player input + int row = Int32.Parse(Console.ReadLine()); + Console.WriteLine("Enter Column:");//player input + int column = Int32.Parse(Console.ReadLine()); + Console.Clear(); + //check if that spot is taken + if (isTaken(board, row, column)) + { + Console.WriteLine("Sorry that spot is taken"); + } + else//otherwise place a marker and change the turn + { + board[row, column] = playerTurn; + playerTurn = (playerTurn == "X") ? "O" : "X"; + } + + if ((2 % i) == 0) //increment turn every two moves + { + turn++; + } + + string winner = "n"; + if (i >= 3) + { + Console.WriteLine("checking winner"); + winner = checkWin(board); + } + + if (i >= 7) + { + winner = checkTie(board); + } + + if (winner == "X" || winner == "O") + { + win = true; + break; + } + } + + return; + } + } + + public static bool isTaken(string[,] board, int row, int column)//checks to see if a given tic tac toe spot is occupied + { + if (String.IsNullOrEmpty(board[row, column])) + { + return false; + } + else + { + return true; + } + } + + public static string checkWin(string[,] board) + { + string checkRows = checkHorizontal(board); + string checkColumns = checkVertical(board); + string checkCorner2Corner = checkDiagonal(board); + if (checkRows == "X" || checkColumns == "X" || checkCorner2Corner == "X") + { + return "X"; + } + else if (checkRows == "O" || checkColumns == "O" || checkCorner2Corner == "O") + { + return "O"; + } + else + { + return "n"; + } + } + public static string checkHorizontal(string[,] board) + { + string isWinner = "n"; + for (int i = 0; i <= 2; i++) + { + if (board[i, 0] == board[i, 1] && board[i, 0] == board[i, 2] && !String.IsNullOrEmpty(board[i, 0])) + { + isWinner = board[i, 0]; + } + } + Console.WriteLine(isWinner); + return isWinner; + } + + public static string checkVertical(string[,] board) + { + string isWinner = "n"; + for (int i = 0; i <= 2; i++) + { + if (board[0, i] == board[1, i] && board[0, i] == board[2, i] && !String.IsNullOrEmpty(board[0, i])) + { + isWinner = board[0, i]; + } + } + return isWinner; + + } + + public static string checkDiagonal(string[,] board) + { + string isWinner = "n"; + if (board[0, 0] == board[1, 1] && board[0, 0] == board[2, 2] && !String.IsNullOrEmpty(board[0, 0])) + { + isWinner = board[0, 0]; + } + else if (board[0, 2] == board[1, 1] && board[0, 2] == board[2, 0] && !String.IsNullOrEmpty(board[0, 2])) + { + isWinner = board[0, 2]; + } + return isWinner; + } + + public static string checkTie(string[,] board) + { + + return "n"; + } + + public static void printBoard(string[,] board) + { + Console.WriteLine(" " + "0" + " 1 " + "2"); + for (int i = 0; i <= 2; i++) + { + if (i != 2) + { + + Console.WriteLine(i + " " + unlessNull(board[i, 0]) + "|" + unlessNull(board[i, 1]) + "|" + unlessNull(board[i, 2])); + Console.WriteLine(" " + "-----"); + } + else + { + Console.WriteLine(i + " " + unlessNull(board[i, 0]) + "|" + unlessNull(board[i, 1]) + "|" + unlessNull(board[i, 2])); + } + } + } + + public static string unlessNull(string checkValue) + { + if (string.IsNullOrEmpty(checkValue)) + { + return " "; + } + else + { + return checkValue; + } + } + } } From 9e923321fca9b10f38a3e62a27d3ea4a4f79cbb4 Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 19 Jan 2019 21:33:38 -0600 Subject: [PATCH 04/19] finished recursive play, score keeping, a basic AI. still to go... give the AI some skills, make ties happen if the remaiining moves result in a tie --- keepScoreTicTT/Program.cs | 231 ++++++++++++++++++++++++++++---------- 1 file changed, 174 insertions(+), 57 deletions(-) diff --git a/keepScoreTicTT/Program.cs b/keepScoreTicTT/Program.cs index 56f947f2..98590d85 100644 --- a/keepScoreTicTT/Program.cs +++ b/keepScoreTicTT/Program.cs @@ -6,76 +6,137 @@ class Program { public static void Main() { - playGame(); + playGame(0, 0, 0); } - public static void playGame() + public static void playGame(int x, int o, int t) { - bool win = false; //If the win is true, game is over + //keep track of wins and ties + int xWins = x; + int oWins = o; + int timesTied = t; + + + bool win = false; + string playerTurn = "X"; + string[,] board = new string[3, 3]; + bool automaticPlay = true; + bool playAgain = true; + //keeps track of the question of manual turns + bool marker = true; + //keeps track of a full turn rotation + int turnTicker = 1; + int turn = 1; while (!win) { - string playerTurn = "X"; - int turn = 0; - string[,] board = new string[3, 3]; + //Make the Board + printBoard(board); - for (int i = 1; i <= 9; i++) + //determine if its an NPC or human, and collect the row and column choice. + int row; + int column; + if (automaticPlay) { - printBoard(board); //Make the Board - Console.WriteLine("Player " + playerTurn); - Console.WriteLine("Enter Row:"); //player input - int row = Int32.Parse(Console.ReadLine()); - Console.WriteLine("Enter Column:");//player input - int column = Int32.Parse(Console.ReadLine()); - Console.Clear(); - //check if that spot is taken - if (isTaken(board, row, column)) - { - Console.WriteLine("Sorry that spot is taken"); - } - else//otherwise place a marker and change the turn - { - board[row, column] = playerTurn; - playerTurn = (playerTurn == "X") ? "O" : "X"; - } + row = autoSelect(); + column = autoSelect(); + string computerGuess = (playerTurn + " guess is: " + row.ToString() + " , " + column.ToString()); + Console.WriteLine(); + Console.WriteLine(computerGuess); + Console.WriteLine(); + } + else + { + row = manualSelect(playerTurn, marker); + marker = false; + column = manualSelect(playerTurn, marker); + marker = true; + } - if ((2 % i) == 0) //increment turn every two moves - { - turn++; - } + //check if that spot is taken + if (isTaken(board, row, column)) + { + Console.WriteLine("Sorry that spot is taken"); + } + else + { + board[row, column] = playerTurn; + playerTurn = (playerTurn == "X") ? "O" : "X"; + turnTicker++; + } - string winner = "n"; - if (i >= 3) - { - Console.WriteLine("checking winner"); - winner = checkWin(board); - } + //increment "turn" every two moves + if ((turnTicker % 2) == 0) + { + turn++; + } - if (i >= 7) - { - winner = checkTie(board); - } + //check for win + string winner = "n"; + string tie = "n"; + if (turn >= 3) + { + tie = checkTie(board); + winner = checkWin(board); + } - if (winner == "X" || winner == "O") - { - win = true; - break; - } + if (winner == "X") + { + xWins++; + Console.Clear(); + printBoard(board); + Console.WriteLine("X is the winner!"); + Console.WriteLine(); + Console.WriteLine("There have been {0} Ties the score is", timesTied); + Console.WriteLine("X: {0}", xWins); + Console.WriteLine("O: {0}", oWins); + System.Threading.Thread.Sleep(10000); + win = true; + } + else if (winner == "O") + { + oWins++; + Console.Clear(); + printBoard(board); + Console.WriteLine("O is the winner!"); + Console.WriteLine(); + Console.WriteLine("There have been {0} Ties the score is", timesTied); + Console.WriteLine("X: {0}", xWins); + Console.WriteLine("O: {0}", oWins); + System.Threading.Thread.Sleep(10000); + win = true; + } + else if (tie == "T") + { + timesTied++; + Console.Clear(); + printBoard(board); + Console.WriteLine("We have a Tie!"); + Console.WriteLine("There have been {0} Ties the score is", timesTied); + Console.WriteLine("X: {0}", xWins); + Console.WriteLine("O: {0}", oWins); + System.Threading.Thread.Sleep(10000); + win = true; } - return; + + } + if (playAgain) + { + playGame(xWins, oWins, timesTied); } } + public static bool isTaken(string[,] board, int row, int column)//checks to see if a given tic tac toe spot is occupied { - if (String.IsNullOrEmpty(board[row, column])) + if (!String.IsNullOrEmpty(board[row, column]) || !String.IsNullOrWhiteSpace(board[row, column])) { - return false; + return true; } else { - return true; + return false; } } @@ -84,6 +145,7 @@ public static string checkWin(string[,] board) string checkRows = checkHorizontal(board); string checkColumns = checkVertical(board); string checkCorner2Corner = checkDiagonal(board); + if (checkRows == "X" || checkColumns == "X" || checkCorner2Corner == "X") { return "X"; @@ -102,14 +164,20 @@ public static string checkHorizontal(string[,] board) string isWinner = "n"; for (int i = 0; i <= 2; i++) { - if (board[i, 0] == board[i, 1] && board[i, 0] == board[i, 2] && !String.IsNullOrEmpty(board[i, 0])) + if (!String.IsNullOrWhiteSpace(board[i, 0]) && !String.IsNullOrEmpty(board[i, 0])) { - isWinner = board[i, 0]; + if (board[i, 0] == board[i, 1] && board[i, 0] == board[i, 2]) + { + isWinner = board[i, 0]; + } + } + else + { + isWinner = "n"; } } Console.WriteLine(isWinner); return isWinner; - } public static string checkVertical(string[,] board) @@ -117,23 +185,22 @@ public static string checkVertical(string[,] board) string isWinner = "n"; for (int i = 0; i <= 2; i++) { - if (board[0, i] == board[1, i] && board[0, i] == board[2, i] && !String.IsNullOrEmpty(board[0, i])) + if (board[0, i] == board[1, i] && board[0, i] == board[2, i] && !String.IsNullOrEmpty(board[0, i]) && !String.IsNullOrWhiteSpace(board[0, 0])) { isWinner = board[0, i]; } } return isWinner; - } public static string checkDiagonal(string[,] board) { string isWinner = "n"; - if (board[0, 0] == board[1, 1] && board[0, 0] == board[2, 2] && !String.IsNullOrEmpty(board[0, 0])) + if (board[0, 0] == board[1, 1] && board[0, 0] == board[2, 2] && !String.IsNullOrEmpty(board[0, 0]) && !String.IsNullOrWhiteSpace(board[0, 0])) { isWinner = board[0, 0]; } - else if (board[0, 2] == board[1, 1] && board[0, 2] == board[2, 0] && !String.IsNullOrEmpty(board[0, 2])) + else if (board[0, 2] == board[1, 1] && board[0, 2] == board[2, 0] && !String.IsNullOrEmpty(board[0, 2]) && !String.IsNullOrWhiteSpace(board[0, 2])) { isWinner = board[0, 2]; } @@ -142,8 +209,23 @@ public static string checkDiagonal(string[,] board) public static string checkTie(string[,] board) { - - return "n"; + string isTie = "n"; + for (int i = 0; i <= 2; i++) + { + if (String.IsNullOrWhiteSpace(board[i, 0]) || String.IsNullOrWhiteSpace(board[i, 1]) || String.IsNullOrWhiteSpace(board[i, 2])) + { + if (String.IsNullOrEmpty(board[i, 0]) || String.IsNullOrEmpty(board[i, 1]) || String.IsNullOrEmpty(board[i, 2])) + { + isTie = "n"; + break; + } + } + else + { + isTie = "T"; + } + } + return isTie; } public static void printBoard(string[,] board) @@ -175,6 +257,41 @@ public static string unlessNull(string checkValue) return checkValue; } } - + private static readonly Random getrandom = new Random(); + public static int autoSelect() + { + int aiGuess = getrandom.Next(0, 31); + int guess = 0; + if (aiGuess <= 10) + { + guess = 0; + } + else if (aiGuess <= 20) + { + guess = 1; + } + else + { + guess = 2; + } + return guess; + } + public static int manualSelect(string player, bool marker) + { + int selection; + if (marker) + { + Console.WriteLine("player {0}'s turn!", player); + Console.Write("Enter Row: "); + selection = Int32.Parse(Console.ReadLine()); + } + else + { + Console.Write("Enter Column: "); + selection = Int32.Parse(Console.ReadLine()); + } + return selection; + } } } + From 467d1376b338142d9ef538064ca431c8a6bf2589 Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 19 Jan 2019 21:52:22 -0600 Subject: [PATCH 05/19] fixed some bugs with turn counting --- keepScoreTicTT/Program.cs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/keepScoreTicTT/Program.cs b/keepScoreTicTT/Program.cs index 98590d85..45cdf8c8 100644 --- a/keepScoreTicTT/Program.cs +++ b/keepScoreTicTT/Program.cs @@ -26,8 +26,8 @@ public static void playGame(int x, int o, int t) //keeps track of the question of manual turns bool marker = true; //keeps track of a full turn rotation - int turnTicker = 1; - int turn = 1; + int turnTicker = 0; + int turns = 0; while (!win) { //Make the Board @@ -68,13 +68,13 @@ public static void playGame(int x, int o, int t) //increment "turn" every two moves if ((turnTicker % 2) == 0) { - turn++; + turns++; } //check for win string winner = "n"; string tie = "n"; - if (turn >= 3) + if (turns >= 3) { tie = checkTie(board); winner = checkWin(board); @@ -85,7 +85,7 @@ public static void playGame(int x, int o, int t) xWins++; Console.Clear(); printBoard(board); - Console.WriteLine("X is the winner!"); + Console.WriteLine("After {0} turns X is victorious!", turns); Console.WriteLine(); Console.WriteLine("There have been {0} Ties the score is", timesTied); Console.WriteLine("X: {0}", xWins); @@ -98,7 +98,7 @@ public static void playGame(int x, int o, int t) oWins++; Console.Clear(); printBoard(board); - Console.WriteLine("O is the winner!"); + Console.WriteLine("After {0} turns O is victorious!", turns); Console.WriteLine(); Console.WriteLine("There have been {0} Ties the score is", timesTied); Console.WriteLine("X: {0}", xWins); @@ -111,7 +111,7 @@ public static void playGame(int x, int o, int t) timesTied++; Console.Clear(); printBoard(board); - Console.WriteLine("We have a Tie!"); + Console.WriteLine("After {0} turns We have a Tie!", turns); Console.WriteLine("There have been {0} Ties the score is", timesTied); Console.WriteLine("X: {0}", xWins); Console.WriteLine("O: {0}", oWins); @@ -257,6 +257,8 @@ public static string unlessNull(string checkValue) return checkValue; } } + + private static readonly Random getrandom = new Random(); public static int autoSelect() { From 620bc6cd3030be19e35b5cee68187fc98ad8d9e1 Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 20 Jan 2019 01:59:40 -0600 Subject: [PATCH 06/19] attempting to give the AI some sort of direction. been slow going --- keepScoreTicTT/Program.cs | 151 ++++++++++++++++++++++++++++++-------- 1 file changed, 120 insertions(+), 31 deletions(-) diff --git a/keepScoreTicTT/Program.cs b/keepScoreTicTT/Program.cs index 45cdf8c8..54ec5d27 100644 --- a/keepScoreTicTT/Program.cs +++ b/keepScoreTicTT/Program.cs @@ -6,28 +6,35 @@ class Program { public static void Main() { - playGame(0, 0, 0); - + bool vsAI = true; + int level = 1; + playGame(0, 0, 0, "x", vsAI, level); } - public static void playGame(int x, int o, int t) + public static void playGame(int x, int o, int t, string previousWinner, bool vsAI, int level) { //keep track of wins and ties int xWins = x; int oWins = o; int timesTied = t; - - + //gameplay conditions bool win = false; string playerTurn = "X"; + if (previousWinner == "X" || previousWinner == "O") + { + playerTurn = (previousWinner == "X") ? "X" : "O"; + } + string winner = "n"; + string tie = "n"; string[,] board = new string[3, 3]; + bool automaticPlay = true; bool playAgain = true; //keeps track of the question of manual turns bool marker = true; //keeps track of a full turn rotation int turnTicker = 0; - int turns = 0; + int turns = 1; while (!win) { //Make the Board @@ -36,10 +43,10 @@ public static void playGame(int x, int o, int t) //determine if its an NPC or human, and collect the row and column choice. int row; int column; - if (automaticPlay) + if (automaticPlay && playerTurn == "O") { - row = autoSelect(); - column = autoSelect(); + row = autoSelect(board, level); + column = autoSelect(board, level); string computerGuess = (playerTurn + " guess is: " + row.ToString() + " , " + column.ToString()); Console.WriteLine(); Console.WriteLine(computerGuess); @@ -63,17 +70,13 @@ public static void playGame(int x, int o, int t) board[row, column] = playerTurn; playerTurn = (playerTurn == "X") ? "O" : "X"; turnTicker++; - } - - //increment "turn" every two moves - if ((turnTicker % 2) == 0) - { - turns++; + if ((turnTicker % 2) == 0) + { + turns++; + } } //check for win - string winner = "n"; - string tie = "n"; if (turns >= 3) { tie = checkTie(board); @@ -123,7 +126,7 @@ public static void playGame(int x, int o, int t) } if (playAgain) { - playGame(xWins, oWins, timesTied); + playGame(xWins, oWins, timesTied, winner, automaticPlay, level); } } @@ -257,10 +260,30 @@ public static string unlessNull(string checkValue) return checkValue; } } - - + public static int manualSelect(string player, bool marker) + { + int selection; + if (marker) + { + Console.WriteLine("player {0}'s turn!", player); + Console.Write("Enter Row: "); + selection = Int32.Parse(Console.ReadLine()); + } + else + { + Console.Write("Enter Column: "); + selection = Int32.Parse(Console.ReadLine()); + } + return selection; + } + //The "AI" private static readonly Random getrandom = new Random(); - public static int autoSelect() + public static int autoSelect(string[,] board, int level, string playerTurn) + { + return easyAI(); + } + + public static int easyAI() { int aiGuess = getrandom.Next(0, 31); int guess = 0; @@ -278,22 +301,88 @@ public static int autoSelect() } return guess; } - public static int manualSelect(string player, bool marker) + + public static int mediumAI(string[,] board, string playerTurn) { - int selection; - if (marker) + string checkForWin = lookForWin(board, playerTurn); + } + + public static string lookForWin(string[,] board, string playerTurn) + { + string potentialRowVictory = checkRowpotential(board, playerTurn); + string potentialColumnVictory = checkColumnpotential(board, playerTurn); + } + public static string checkRowpotential(string[,] board, string playerTurn) + { + string valueToReturn = "3,3"; ; + for (int i = 0; i < 3; i++) { - Console.WriteLine("player {0}'s turn!", player); - Console.Write("Enter Row: "); - selection = Int32.Parse(Console.ReadLine()); + if (String.IsNullOrWhiteSpace(board[i, 0])) + { + if (board[i, 1] == board[i, 2] && board[i, 1] == playerTurn) + { + valueToReturn = i + ",0"; + } + } + else if (String.IsNullOrWhiteSpace(board[i, 1])) + { + if (board[i, 0] == board[i, 2] && board[i, 0] == playerTurn) + { + + valueToReturn = i + ",1"; + } + } + else if (String.IsNullOrWhiteSpace(board[i, 2])) + { + if (board[i, 1] == board[i, 0] && board[i, 1] == playerTurn) + { + + valueToReturn = i + ",2"; + } + } + else + { + valueToReturn = "3,3"; + } } - else + return valueToReturn; + } + public static string checkColumnpotential(string[,] board, string playerTurn) + { + string valueToReturn = "3,3"; ; + for (int i = 0; i < 3; i++) { - Console.Write("Enter Column: "); - selection = Int32.Parse(Console.ReadLine()); + if (String.IsNullOrWhiteSpace(board[0, i])) + { + if (board[1, i] == board[2, i] && board[1, i] == playerTurn) + { + valueToReturn = "0," + i; + } + } + else if (String.IsNullOrWhiteSpace(board[1, i])) + { + if (board[0, i] == board[2, i] && board[0, i] == playerTurn) + { + + valueToReturn = "1," + i; + } + } + else if (String.IsNullOrWhiteSpace(board[i, 2])) + { + if (board[i, 1] == board[i, 0] && board[i, 1] == playerTurn) + { + + valueToReturn = i + ",2"; + } + } + else + { + valueToReturn = "3,3"; + } } - return selection; + return valueToReturn; } } } + From 5c5fab232ce53ac0032b1e15d896d91648c71dc8 Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 20 Jan 2019 13:51:56 -0600 Subject: [PATCH 07/19] still working on AI, finished checks for win and working on checks for block --- keepScoreTicTT/.vscode/launch.json | 28 ++++++++++++++++++++++++++++ keepScoreTicTT/.vscode/tasks.json | 15 +++++++++++++++ keepScoreTicTT/bash.exe.stackdump | 16 ++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 keepScoreTicTT/.vscode/launch.json create mode 100644 keepScoreTicTT/.vscode/tasks.json create mode 100644 keepScoreTicTT/bash.exe.stackdump diff --git a/keepScoreTicTT/.vscode/launch.json b/keepScoreTicTT/.vscode/launch.json new file mode 100644 index 00000000..d51caff3 --- /dev/null +++ b/keepScoreTicTT/.vscode/launch.json @@ -0,0 +1,28 @@ +{ + // Use IntelliSense to find out which attributes exist for C# debugging + // Use hover for the description of the existing attributes + // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md + "version": "0.2.0", + "configurations": [ + { + "name": ".NET Core Launch (console)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + // If you have changed target frameworks, make sure to update the program path. + "program": "${workspaceFolder}/bin/Debug/netcoreapp2.2/keepScoreTicTT.dll", + "args": [], + "cwd": "${workspaceFolder}", + // For more information about the 'console' field, see https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window + "console": "integratedTerminal", + "stopAtEntry": false, + "internalConsoleOptions": "openOnSessionStart" + }, + { + "name": ".NET Core Attach", + "type": "coreclr", + "request": "attach", + "processId": "${command:pickProcess}" + }, + ] +} \ No newline at end of file diff --git a/keepScoreTicTT/.vscode/tasks.json b/keepScoreTicTT/.vscode/tasks.json new file mode 100644 index 00000000..f1262514 --- /dev/null +++ b/keepScoreTicTT/.vscode/tasks.json @@ -0,0 +1,15 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/keepScoreTicTT.csproj" + ], + "problemMatcher": "$msCompile" + } + ] +} \ No newline at end of file diff --git a/keepScoreTicTT/bash.exe.stackdump b/keepScoreTicTT/bash.exe.stackdump new file mode 100644 index 00000000..4bc86685 --- /dev/null +++ b/keepScoreTicTT/bash.exe.stackdump @@ -0,0 +1,16 @@ +Stack trace: +Frame Function Args +00180328AF0 0018006021E (00180247D00, 001802340B9, 00000000058, 000FFFFB740) +00180328AF0 00180048859 (00180236765, 00100000000, 00000000000, 00000000001) +00180328AF0 00180048892 (00000000000, 00000000000, 00000000058, 00180328950) +00180328AF0 0018006C179 (0000000000A, 00000000000, 0000000000A, 00000000000) +00180328AF0 0018006C342 (00000000003, 00000000000, 00180044EEF, 000FFFFCB30) +00000000000 0018006D3A8 (0000000000D, 000FFFFC920, 001800E4CF6, 000FFFFC920) +00000000000 00180058816 (000FFFF0000, 00000000000, 00000000000, 006FFFFFFFF) +00000000000 001800590A9 (000FFFFCAF0, 00600040000, 00000000000, 000FFFFCB80) +00180325CF8 001800595BA (001800C0322, 00000000000, 00000000000, 00000000000) +000FFFFCCD0 00180059937 (000FFFFCDF0, 000FFFFCCD0, FFFFFFFFFFFFFFD1, 00000000000) +000FFFFCCD0 00180048FE1 (00000000000, 00000000000, 00000000000, 00000000000) +00000000000 00180047963 (00000000000, 00000000000, 00000000000, 00000000000) +000FFFFFFF0 00180047A14 (00000000000, 00000000000, 00000000000, 00000000000) +End of stack trace From 81b8a89231c62eb4286dd2df0db69de7bce277ee Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 20 Jan 2019 15:25:22 -0600 Subject: [PATCH 08/19] finishing up medium dificulty, checking a diag potential victory has proven troublesome. still working on that --- keepScoreTicTT/Program.cs | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/keepScoreTicTT/Program.cs b/keepScoreTicTT/Program.cs index 54ec5d27..ab93d8bd 100644 --- a/keepScoreTicTT/Program.cs +++ b/keepScoreTicTT/Program.cs @@ -45,8 +45,9 @@ public static void playGame(int x, int o, int t, string previousWinner, bool vsA int column; if (automaticPlay && playerTurn == "O") { - row = autoSelect(board, level); - column = autoSelect(board, level); + string selectedSpot = autoSelect(board, level, playerTurn); + column = 1; + row = 1; string computerGuess = (playerTurn + " guess is: " + row.ToString() + " , " + column.ToString()); Console.WriteLine(); Console.WriteLine(computerGuess); @@ -278,9 +279,15 @@ public static int manualSelect(string player, bool marker) } //The "AI" private static readonly Random getrandom = new Random(); - public static int autoSelect(string[,] board, int level, string playerTurn) + public static string autoSelect(string[,] board, int level, string playerTurn) { - return easyAI(); + + string computerGuess = "n"; + if (level == 0) + { + computerGuess = easyAI() + "," + easyAI(); + } + return computerGuess; } public static int easyAI() @@ -305,6 +312,9 @@ public static int easyAI() public static int mediumAI(string[,] board, string playerTurn) { string checkForWin = lookForWin(board, playerTurn); + + string opponentTurn = (playerTurn == "X") ? "O" : "X"; + string checkForBlock = lookForWin(board, opponentTurn); } public static string lookForWin(string[,] board, string playerTurn) @@ -314,7 +324,7 @@ public static string lookForWin(string[,] board, string playerTurn) } public static string checkRowpotential(string[,] board, string playerTurn) { - string valueToReturn = "3,3"; ; + string valueToReturn = "n"; ; for (int i = 0; i < 3; i++) { if (String.IsNullOrWhiteSpace(board[i, 0])) @@ -342,14 +352,14 @@ public static string checkRowpotential(string[,] board, string playerTurn) } else { - valueToReturn = "3,3"; + valueToReturn = "n"; } } return valueToReturn; } public static string checkColumnpotential(string[,] board, string playerTurn) { - string valueToReturn = "3,3"; ; + string valueToReturn = "n"; ; for (int i = 0; i < 3; i++) { if (String.IsNullOrWhiteSpace(board[0, i])) @@ -372,12 +382,12 @@ public static string checkColumnpotential(string[,] board, string playerTurn) if (board[i, 1] == board[i, 0] && board[i, 1] == playerTurn) { - valueToReturn = i + ",2"; + valueToReturn = "2," + i; } } else { - valueToReturn = "3,3"; + valueToReturn = "n"; } } return valueToReturn; From 5c1bb8ded24b1e8263b841b86eb4f58a8c1ecf46 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 21 Jan 2019 01:25:18 -0600 Subject: [PATCH 09/19] working code with 2 levels of AI and choices for which player or both are AI need to finish 3rd level of AI a main menu, the ability to view a specific winning board or all boards or board from x to x possibly some other ideas. Tested medium 'AI' vs easy 'AI' and its no contest medium wins consistantly but is still beatable to this point it will find if there is a win condition this turn and if that fails it will attempt to locate if the oposing player has a win condition on its next turn and block it before chooseing --- keepScoreTicTT/Program.cs | 218 +++++++++++++++++++++++++++++++++++--- 1 file changed, 203 insertions(+), 15 deletions(-) diff --git a/keepScoreTicTT/Program.cs b/keepScoreTicTT/Program.cs index ab93d8bd..f0a29c8b 100644 --- a/keepScoreTicTT/Program.cs +++ b/keepScoreTicTT/Program.cs @@ -6,12 +6,52 @@ class Program { public static void Main() { - bool vsAI = true; - int level = 1; - playGame(0, 0, 0, "x", vsAI, level); + bool playerOneAI = false; + bool playerTwoAI = true; + int levelOne = 1; + int levelTwo = 1; + + Console.WriteLine("Is player 1 an AI y/n"); + string answer = Console.ReadLine(); + if (answer.Substring(0, 1).ToLower() == "y") + { + playerOneAI = true; + Console.WriteLine("Which difficulty level"); + Console.WriteLine("Easy: 0 | Medium: 1 | Hard: 2"); + int difficultyRequest = Convert.ToInt32(Console.ReadLine()); + if (difficultyRequest == 0 || Convert.ToInt32(difficultyRequest) == 1 || Convert.ToInt32(difficultyRequest) == 2) + { + levelOne = Convert.ToInt32(difficultyRequest); + } + + } + else + { + playerOneAI = false; + } + Console.WriteLine("Is player 2 an AI y/n"); + string answerTwo = Console.ReadLine(); + if (answer.Substring(0, 1).ToLower() == "y") + { + playerOneAI = true; + Console.WriteLine("Which difficulty level"); + Console.WriteLine("Easy: 0 | Medium: 1 | Hard: 2"); + int difficultyRequest = Convert.ToInt32(Console.ReadLine()); + if (difficultyRequest == 0 || Convert.ToInt32(difficultyRequest) == 1 || Convert.ToInt32(difficultyRequest) == 2) + { + levelTwo = Convert.ToInt32(difficultyRequest); + } + + } + else + { + playerOneAI = false; + } + + playGame(0, 0, 0, "x", playerOneAI, playerTwoAI, levelOne, levelTwo); } - public static void playGame(int x, int o, int t, string previousWinner, bool vsAI, int level) + public static void playGame(int x, int o, int t, string previousWinner, bool playerOneAI, bool playerTwoAI, int levelOne, int levelTwo) { //keep track of wins and ties int xWins = x; @@ -28,7 +68,10 @@ public static void playGame(int x, int o, int t, string previousWinner, bool vsA string tie = "n"; string[,] board = new string[3, 3]; - bool automaticPlay = true; + bool isOneAI = playerOneAI; + bool isTwoAI = playerTwoAI; + int playerOnelevel = levelOne; + int playerTwolevel = levelTwo; bool playAgain = true; //keeps track of the question of manual turns bool marker = true; @@ -43,11 +86,23 @@ public static void playGame(int x, int o, int t, string previousWinner, bool vsA //determine if its an NPC or human, and collect the row and column choice. int row; int column; - if (automaticPlay && playerTurn == "O") + if (isTwoAI && playerTurn == "O") + { + string selectedSpot = autoSelect(board, playerTwolevel, playerTurn); + + row = (Convert.ToInt32(selectedSpot.Substring(0, 1))); + column = (Convert.ToInt32(selectedSpot.Substring(2, 1))); ; + string computerGuess = (playerTurn + " guess is: " + row.ToString() + " , " + column.ToString()); + Console.WriteLine(); + Console.WriteLine(computerGuess); + Console.WriteLine(); + } + else if (isOneAI && playerTurn == "X") { - string selectedSpot = autoSelect(board, level, playerTurn); - column = 1; - row = 1; + string selectedSpot = autoSelect(board, playerOnelevel, playerTurn); + + row = (Convert.ToInt32(selectedSpot.Substring(0, 1))); + column = (Convert.ToInt32(selectedSpot.Substring(2, 1))); ; string computerGuess = (playerTurn + " guess is: " + row.ToString() + " , " + column.ToString()); Console.WriteLine(); Console.WriteLine(computerGuess); @@ -127,7 +182,7 @@ public static void playGame(int x, int o, int t, string previousWinner, bool vsA } if (playAgain) { - playGame(xWins, oWins, timesTied, winner, automaticPlay, level); + playGame(xWins, oWins, timesTied, winner, isOneAI, isTwoAI, playerOnelevel, playerTwolevel); } } @@ -277,8 +332,9 @@ public static int manualSelect(string player, bool marker) } return selection; } - //The "AI" + //The "AI" 3 levels private static readonly Random getrandom = new Random(); + public static string autoSelect(string[,] board, int level, string playerTurn) { @@ -287,6 +343,11 @@ public static string autoSelect(string[,] board, int level, string playerTurn) { computerGuess = easyAI() + "," + easyAI(); } + else if (level == 1) + { + computerGuess = mediumAI(board, playerTurn); + } + //return appropriate level AI's choice return computerGuess; } @@ -309,22 +370,52 @@ public static int easyAI() return guess; } - public static int mediumAI(string[,] board, string playerTurn) + public static string mediumAI(string[,] board, string playerTurn) { - string checkForWin = lookForWin(board, playerTurn); - string opponentTurn = (playerTurn == "X") ? "O" : "X"; + string checkForWin = lookForWin(board, playerTurn); string checkForBlock = lookForWin(board, opponentTurn); + + if (checkForWin != "n") + { + return checkForWin; + } + else if (checkForBlock != "n") + { + return checkForBlock; + } + else + { + return easyAI() + "," + easyAI(); + } } public static string lookForWin(string[,] board, string playerTurn) { string potentialRowVictory = checkRowpotential(board, playerTurn); string potentialColumnVictory = checkColumnpotential(board, playerTurn); + string potentialDiagVictory = checkDiagPotential(board, playerTurn); + + string returnValue = "n"; + + //check each + if (potentialRowVictory != "n") + { + returnValue = potentialRowVictory; + } + else if (potentialColumnVictory != "n") + { + returnValue = potentialColumnVictory; + } + else if (potentialDiagVictory != "n") + { + returnValue = potentialDiagVictory; + } + return returnValue; } public static string checkRowpotential(string[,] board, string playerTurn) { - string valueToReturn = "n"; ; + string valueToReturn = "n"; for (int i = 0; i < 3; i++) { if (String.IsNullOrWhiteSpace(board[i, 0])) @@ -332,6 +423,12 @@ public static string checkRowpotential(string[,] board, string playerTurn) if (board[i, 1] == board[i, 2] && board[i, 1] == playerTurn) { valueToReturn = i + ",0"; + int row = (Convert.ToInt32(valueToReturn.Substring(0, 1))); + int column = (Convert.ToInt32(valueToReturn.Substring(2, 1))); ; + if (isTaken(board, row, column)) + { + valueToReturn = "n"; + } } } else if (String.IsNullOrWhiteSpace(board[i, 1])) @@ -340,6 +437,12 @@ public static string checkRowpotential(string[,] board, string playerTurn) { valueToReturn = i + ",1"; + int row = (Convert.ToInt32(valueToReturn.Substring(0, 1))); + int column = (Convert.ToInt32(valueToReturn.Substring(2, 1))); ; + if (isTaken(board, row, column)) + { + valueToReturn = "n"; + } } } else if (String.IsNullOrWhiteSpace(board[i, 2])) @@ -348,11 +451,18 @@ public static string checkRowpotential(string[,] board, string playerTurn) { valueToReturn = i + ",2"; + int row = (Convert.ToInt32(valueToReturn.Substring(0, 1))); + int column = (Convert.ToInt32(valueToReturn.Substring(2, 1))); ; + if (isTaken(board, row, column)) + { + valueToReturn = "n"; + } } } else { valueToReturn = "n"; + } } return valueToReturn; @@ -367,6 +477,12 @@ public static string checkColumnpotential(string[,] board, string playerTurn) if (board[1, i] == board[2, i] && board[1, i] == playerTurn) { valueToReturn = "0," + i; + int row = (Convert.ToInt32(valueToReturn.Substring(0, 1))); + int column = (Convert.ToInt32(valueToReturn.Substring(2, 1))); ; + if (isTaken(board, row, column)) + { + valueToReturn = "n"; + } } } else if (String.IsNullOrWhiteSpace(board[1, i])) @@ -375,6 +491,12 @@ public static string checkColumnpotential(string[,] board, string playerTurn) { valueToReturn = "1," + i; + int row = (Convert.ToInt32(valueToReturn.Substring(0, 1))); + int column = (Convert.ToInt32(valueToReturn.Substring(2, 1))); ; + if (isTaken(board, row, column)) + { + valueToReturn = "n"; + } } } else if (String.IsNullOrWhiteSpace(board[i, 2])) @@ -383,6 +505,12 @@ public static string checkColumnpotential(string[,] board, string playerTurn) { valueToReturn = "2," + i; + int row = (Convert.ToInt32(valueToReturn.Substring(0, 1))); + int column = (Convert.ToInt32(valueToReturn.Substring(2, 1))); ; + if (isTaken(board, row, column)) + { + valueToReturn = "n"; + } } } else @@ -392,7 +520,67 @@ public static string checkColumnpotential(string[,] board, string playerTurn) } return valueToReturn; } + public static string checkDiagPotential(string[,] board, string playerTurn) + { + string valueToReturn = "n"; ; + string[,] testBoard = board; + for (int i = 0; i > 2; i++) + { + testBoard[i, i] = playerTurn; + if (checkDiagonal(testBoard) != "n") + { + valueToReturn = i.ToString() + "," + i.ToString(); + int row = (Convert.ToInt32(valueToReturn.Substring(0, 1))); + int column = (Convert.ToInt32(valueToReturn.Substring(2, 1))); ; + if (isTaken(board, row, column)) + { + valueToReturn = "n"; + } + } + testBoard[i, i] = board[i, i]; + } + if (valueToReturn != "n") + { + testBoard[0, 2] = playerTurn; + if (checkDiagonal(testBoard) != "n") + { + valueToReturn = "0,2"; + int row = (Convert.ToInt32(valueToReturn.Substring(0, 1))); + int column = (Convert.ToInt32(valueToReturn.Substring(2, 1))); ; + if (isTaken(board, row, column)) + { + valueToReturn = "n"; + } + } + testBoard[0, 2] = board[0, 2]; + testBoard[1, 1] = playerTurn; + if (checkDiagonal(testBoard) != "n") + { + valueToReturn = "1,1"; + int row = (Convert.ToInt32(valueToReturn.Substring(0, 1))); + int column = (Convert.ToInt32(valueToReturn.Substring(2, 1))); ; + if (isTaken(board, row, column)) + { + valueToReturn = "n"; + } + } + testBoard[1, 1] = board[1, 1]; + testBoard[2, 0] = playerTurn; + if (checkDiagonal(testBoard) != "n") + { + valueToReturn = "2,0"; + int row = (Convert.ToInt32(valueToReturn.Substring(0, 1))); + int column = (Convert.ToInt32(valueToReturn.Substring(2, 1))); ; + if (isTaken(board, row, column)) + { + valueToReturn = "n"; + } + } + } + return valueToReturn; + } } } + From 6b71ec36827f1393dda70379c64b01e350eaaaf8 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 21 Jan 2019 02:40:36 -0600 Subject: [PATCH 10/19] built main menu does not yet launch gameor check results --- keepScoreTicTT/Program.cs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/keepScoreTicTT/Program.cs b/keepScoreTicTT/Program.cs index f0a29c8b..64e6678c 100644 --- a/keepScoreTicTT/Program.cs +++ b/keepScoreTicTT/Program.cs @@ -5,6 +5,20 @@ namespace TicTacToe class Program { public static void Main() + { + mainMenu(); + } + public static void mainMenu() + { + Console.WriteLine("*************************************************"); + Console.WriteLine("* Main Menu *"); + Console.WriteLine("* *"); + Console.WriteLine("* Play Games: p View Games: v *"); + Console.WriteLine("* *"); + Console.WriteLine("*************************************************"); + + } + public static void setupGame() { bool playerOneAI = false; bool playerTwoAI = true; @@ -47,7 +61,8 @@ public static void Main() { playerOneAI = false; } - + // how many times do you want the game to run then loop that many times + //string pullResults = playGame(0, 0, 0, "x", playerOneAI, playerTwoAI, levelOne, levelTwo); } From 05749682ddcc237d5c69ae07b699ce400267ab9b Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 23 Jan 2019 16:02:39 -0600 Subject: [PATCH 11/19] finished seperating the files --- keepScoreTicTT/Program.cs | 587 +------------------------------------- 1 file changed, 3 insertions(+), 584 deletions(-) diff --git a/keepScoreTicTT/Program.cs b/keepScoreTicTT/Program.cs index 64e6678c..bfd7de81 100644 --- a/keepScoreTicTT/Program.cs +++ b/keepScoreTicTT/Program.cs @@ -1,4 +1,5 @@ using System; +using System.Text; namespace TicTacToe { @@ -6,596 +7,14 @@ class Program { public static void Main() { - mainMenu(); + string[] returnedResults = new string[1]; + mainMenu(returnedResults); } - public static void mainMenu() - { - Console.WriteLine("*************************************************"); - Console.WriteLine("* Main Menu *"); - Console.WriteLine("* *"); - Console.WriteLine("* Play Games: p View Games: v *"); - Console.WriteLine("* *"); - Console.WriteLine("*************************************************"); - - } - public static void setupGame() - { - bool playerOneAI = false; - bool playerTwoAI = true; - int levelOne = 1; - int levelTwo = 1; - - Console.WriteLine("Is player 1 an AI y/n"); - string answer = Console.ReadLine(); - if (answer.Substring(0, 1).ToLower() == "y") - { - playerOneAI = true; - Console.WriteLine("Which difficulty level"); - Console.WriteLine("Easy: 0 | Medium: 1 | Hard: 2"); - int difficultyRequest = Convert.ToInt32(Console.ReadLine()); - if (difficultyRequest == 0 || Convert.ToInt32(difficultyRequest) == 1 || Convert.ToInt32(difficultyRequest) == 2) - { - levelOne = Convert.ToInt32(difficultyRequest); - } - - } - else - { - playerOneAI = false; - } - Console.WriteLine("Is player 2 an AI y/n"); - string answerTwo = Console.ReadLine(); - if (answer.Substring(0, 1).ToLower() == "y") - { - playerOneAI = true; - Console.WriteLine("Which difficulty level"); - Console.WriteLine("Easy: 0 | Medium: 1 | Hard: 2"); - int difficultyRequest = Convert.ToInt32(Console.ReadLine()); - if (difficultyRequest == 0 || Convert.ToInt32(difficultyRequest) == 1 || Convert.ToInt32(difficultyRequest) == 2) - { - levelTwo = Convert.ToInt32(difficultyRequest); - } - - } - else - { - playerOneAI = false; - } - // how many times do you want the game to run then loop that many times - //string pullResults = - playGame(0, 0, 0, "x", playerOneAI, playerTwoAI, levelOne, levelTwo); - } - - public static void playGame(int x, int o, int t, string previousWinner, bool playerOneAI, bool playerTwoAI, int levelOne, int levelTwo) - { - //keep track of wins and ties - int xWins = x; - int oWins = o; - int timesTied = t; - //gameplay conditions - bool win = false; - string playerTurn = "X"; - if (previousWinner == "X" || previousWinner == "O") - { - playerTurn = (previousWinner == "X") ? "X" : "O"; - } - string winner = "n"; - string tie = "n"; - string[,] board = new string[3, 3]; - - bool isOneAI = playerOneAI; - bool isTwoAI = playerTwoAI; - int playerOnelevel = levelOne; - int playerTwolevel = levelTwo; - bool playAgain = true; - //keeps track of the question of manual turns - bool marker = true; - //keeps track of a full turn rotation - int turnTicker = 0; - int turns = 1; - while (!win) - { - //Make the Board - printBoard(board); - - //determine if its an NPC or human, and collect the row and column choice. - int row; - int column; - if (isTwoAI && playerTurn == "O") - { - string selectedSpot = autoSelect(board, playerTwolevel, playerTurn); - - row = (Convert.ToInt32(selectedSpot.Substring(0, 1))); - column = (Convert.ToInt32(selectedSpot.Substring(2, 1))); ; - string computerGuess = (playerTurn + " guess is: " + row.ToString() + " , " + column.ToString()); - Console.WriteLine(); - Console.WriteLine(computerGuess); - Console.WriteLine(); - } - else if (isOneAI && playerTurn == "X") - { - string selectedSpot = autoSelect(board, playerOnelevel, playerTurn); - - row = (Convert.ToInt32(selectedSpot.Substring(0, 1))); - column = (Convert.ToInt32(selectedSpot.Substring(2, 1))); ; - string computerGuess = (playerTurn + " guess is: " + row.ToString() + " , " + column.ToString()); - Console.WriteLine(); - Console.WriteLine(computerGuess); - Console.WriteLine(); - } - else - { - row = manualSelect(playerTurn, marker); - marker = false; - column = manualSelect(playerTurn, marker); - marker = true; - } - - //check if that spot is taken - if (isTaken(board, row, column)) - { - Console.WriteLine("Sorry that spot is taken"); - } - else - { - board[row, column] = playerTurn; - playerTurn = (playerTurn == "X") ? "O" : "X"; - turnTicker++; - if ((turnTicker % 2) == 0) - { - turns++; - } - } - - //check for win - if (turns >= 3) - { - tie = checkTie(board); - winner = checkWin(board); - } - - if (winner == "X") - { - xWins++; - Console.Clear(); - printBoard(board); - Console.WriteLine("After {0} turns X is victorious!", turns); - Console.WriteLine(); - Console.WriteLine("There have been {0} Ties the score is", timesTied); - Console.WriteLine("X: {0}", xWins); - Console.WriteLine("O: {0}", oWins); - System.Threading.Thread.Sleep(10000); - win = true; - } - else if (winner == "O") - { - oWins++; - Console.Clear(); - printBoard(board); - Console.WriteLine("After {0} turns O is victorious!", turns); - Console.WriteLine(); - Console.WriteLine("There have been {0} Ties the score is", timesTied); - Console.WriteLine("X: {0}", xWins); - Console.WriteLine("O: {0}", oWins); - System.Threading.Thread.Sleep(10000); - win = true; - } - else if (tie == "T") - { - timesTied++; - Console.Clear(); - printBoard(board); - Console.WriteLine("After {0} turns We have a Tie!", turns); - Console.WriteLine("There have been {0} Ties the score is", timesTied); - Console.WriteLine("X: {0}", xWins); - Console.WriteLine("O: {0}", oWins); - System.Threading.Thread.Sleep(10000); - win = true; - } - - - } - if (playAgain) - { - playGame(xWins, oWins, timesTied, winner, isOneAI, isTwoAI, playerOnelevel, playerTwolevel); - } - } - - - public static bool isTaken(string[,] board, int row, int column)//checks to see if a given tic tac toe spot is occupied - { - if (!String.IsNullOrEmpty(board[row, column]) || !String.IsNullOrWhiteSpace(board[row, column])) - { - return true; - } - else - { - return false; - } - } - - public static string checkWin(string[,] board) - { - string checkRows = checkHorizontal(board); - string checkColumns = checkVertical(board); - string checkCorner2Corner = checkDiagonal(board); - - if (checkRows == "X" || checkColumns == "X" || checkCorner2Corner == "X") - { - return "X"; - } - else if (checkRows == "O" || checkColumns == "O" || checkCorner2Corner == "O") - { - return "O"; - } - else - { - return "n"; - } - } - public static string checkHorizontal(string[,] board) - { - string isWinner = "n"; - for (int i = 0; i <= 2; i++) - { - if (!String.IsNullOrWhiteSpace(board[i, 0]) && !String.IsNullOrEmpty(board[i, 0])) - { - if (board[i, 0] == board[i, 1] && board[i, 0] == board[i, 2]) - { - isWinner = board[i, 0]; - } - } - else - { - isWinner = "n"; - } - } - Console.WriteLine(isWinner); - return isWinner; - } - - public static string checkVertical(string[,] board) - { - string isWinner = "n"; - for (int i = 0; i <= 2; i++) - { - if (board[0, i] == board[1, i] && board[0, i] == board[2, i] && !String.IsNullOrEmpty(board[0, i]) && !String.IsNullOrWhiteSpace(board[0, 0])) - { - isWinner = board[0, i]; - } - } - return isWinner; - } - - public static string checkDiagonal(string[,] board) - { - string isWinner = "n"; - if (board[0, 0] == board[1, 1] && board[0, 0] == board[2, 2] && !String.IsNullOrEmpty(board[0, 0]) && !String.IsNullOrWhiteSpace(board[0, 0])) - { - isWinner = board[0, 0]; - } - else if (board[0, 2] == board[1, 1] && board[0, 2] == board[2, 0] && !String.IsNullOrEmpty(board[0, 2]) && !String.IsNullOrWhiteSpace(board[0, 2])) - { - isWinner = board[0, 2]; - } - return isWinner; - } - - public static string checkTie(string[,] board) - { - string isTie = "n"; - for (int i = 0; i <= 2; i++) - { - if (String.IsNullOrWhiteSpace(board[i, 0]) || String.IsNullOrWhiteSpace(board[i, 1]) || String.IsNullOrWhiteSpace(board[i, 2])) - { - if (String.IsNullOrEmpty(board[i, 0]) || String.IsNullOrEmpty(board[i, 1]) || String.IsNullOrEmpty(board[i, 2])) - { - isTie = "n"; - break; - } - } - else - { - isTie = "T"; - } - } - return isTie; - } - - public static void printBoard(string[,] board) - { - Console.WriteLine(" " + "0" + " 1 " + "2"); - for (int i = 0; i <= 2; i++) - { - if (i != 2) - { - - Console.WriteLine(i + " " + unlessNull(board[i, 0]) + "|" + unlessNull(board[i, 1]) + "|" + unlessNull(board[i, 2])); - Console.WriteLine(" " + "-----"); - } - else - { - Console.WriteLine(i + " " + unlessNull(board[i, 0]) + "|" + unlessNull(board[i, 1]) + "|" + unlessNull(board[i, 2])); - } - } - } - - public static string unlessNull(string checkValue) - { - if (string.IsNullOrEmpty(checkValue)) - { - return " "; - } - else - { - return checkValue; - } - } - public static int manualSelect(string player, bool marker) - { - int selection; - if (marker) - { - Console.WriteLine("player {0}'s turn!", player); - Console.Write("Enter Row: "); - selection = Int32.Parse(Console.ReadLine()); - } - else - { - Console.Write("Enter Column: "); - selection = Int32.Parse(Console.ReadLine()); - } - return selection; - } - //The "AI" 3 levels - private static readonly Random getrandom = new Random(); - public static string autoSelect(string[,] board, int level, string playerTurn) - { - string computerGuess = "n"; - if (level == 0) - { - computerGuess = easyAI() + "," + easyAI(); - } - else if (level == 1) - { - computerGuess = mediumAI(board, playerTurn); - } - //return appropriate level AI's choice - return computerGuess; - } - public static int easyAI() - { - int aiGuess = getrandom.Next(0, 31); - int guess = 0; - if (aiGuess <= 10) - { - guess = 0; - } - else if (aiGuess <= 20) - { - guess = 1; - } - else - { - guess = 2; - } - return guess; - } - public static string mediumAI(string[,] board, string playerTurn) - { - string opponentTurn = (playerTurn == "X") ? "O" : "X"; - string checkForWin = lookForWin(board, playerTurn); - string checkForBlock = lookForWin(board, opponentTurn); - if (checkForWin != "n") - { - return checkForWin; - } - else if (checkForBlock != "n") - { - return checkForBlock; - } - else - { - return easyAI() + "," + easyAI(); - } - } - - public static string lookForWin(string[,] board, string playerTurn) - { - string potentialRowVictory = checkRowpotential(board, playerTurn); - string potentialColumnVictory = checkColumnpotential(board, playerTurn); - string potentialDiagVictory = checkDiagPotential(board, playerTurn); - - string returnValue = "n"; - - //check each - if (potentialRowVictory != "n") - { - returnValue = potentialRowVictory; - } - else if (potentialColumnVictory != "n") - { - returnValue = potentialColumnVictory; - } - else if (potentialDiagVictory != "n") - { - returnValue = potentialDiagVictory; - } - return returnValue; - } - public static string checkRowpotential(string[,] board, string playerTurn) - { - string valueToReturn = "n"; - for (int i = 0; i < 3; i++) - { - if (String.IsNullOrWhiteSpace(board[i, 0])) - { - if (board[i, 1] == board[i, 2] && board[i, 1] == playerTurn) - { - valueToReturn = i + ",0"; - int row = (Convert.ToInt32(valueToReturn.Substring(0, 1))); - int column = (Convert.ToInt32(valueToReturn.Substring(2, 1))); ; - if (isTaken(board, row, column)) - { - valueToReturn = "n"; - } - } - } - else if (String.IsNullOrWhiteSpace(board[i, 1])) - { - if (board[i, 0] == board[i, 2] && board[i, 0] == playerTurn) - { - - valueToReturn = i + ",1"; - int row = (Convert.ToInt32(valueToReturn.Substring(0, 1))); - int column = (Convert.ToInt32(valueToReturn.Substring(2, 1))); ; - if (isTaken(board, row, column)) - { - valueToReturn = "n"; - } - } - } - else if (String.IsNullOrWhiteSpace(board[i, 2])) - { - if (board[i, 1] == board[i, 0] && board[i, 1] == playerTurn) - { - - valueToReturn = i + ",2"; - int row = (Convert.ToInt32(valueToReturn.Substring(0, 1))); - int column = (Convert.ToInt32(valueToReturn.Substring(2, 1))); ; - if (isTaken(board, row, column)) - { - valueToReturn = "n"; - } - } - } - else - { - valueToReturn = "n"; - - } - } - return valueToReturn; - } - public static string checkColumnpotential(string[,] board, string playerTurn) - { - string valueToReturn = "n"; ; - for (int i = 0; i < 3; i++) - { - if (String.IsNullOrWhiteSpace(board[0, i])) - { - if (board[1, i] == board[2, i] && board[1, i] == playerTurn) - { - valueToReturn = "0," + i; - int row = (Convert.ToInt32(valueToReturn.Substring(0, 1))); - int column = (Convert.ToInt32(valueToReturn.Substring(2, 1))); ; - if (isTaken(board, row, column)) - { - valueToReturn = "n"; - } - } - } - else if (String.IsNullOrWhiteSpace(board[1, i])) - { - if (board[0, i] == board[2, i] && board[0, i] == playerTurn) - { - - valueToReturn = "1," + i; - int row = (Convert.ToInt32(valueToReturn.Substring(0, 1))); - int column = (Convert.ToInt32(valueToReturn.Substring(2, 1))); ; - if (isTaken(board, row, column)) - { - valueToReturn = "n"; - } - } - } - else if (String.IsNullOrWhiteSpace(board[i, 2])) - { - if (board[i, 1] == board[i, 0] && board[i, 1] == playerTurn) - { - - valueToReturn = "2," + i; - int row = (Convert.ToInt32(valueToReturn.Substring(0, 1))); - int column = (Convert.ToInt32(valueToReturn.Substring(2, 1))); ; - if (isTaken(board, row, column)) - { - valueToReturn = "n"; - } - } - } - else - { - valueToReturn = "n"; - } - } - return valueToReturn; - } - public static string checkDiagPotential(string[,] board, string playerTurn) - { - string valueToReturn = "n"; ; - string[,] testBoard = board; - for (int i = 0; i > 2; i++) - { - testBoard[i, i] = playerTurn; - if (checkDiagonal(testBoard) != "n") - { - valueToReturn = i.ToString() + "," + i.ToString(); - int row = (Convert.ToInt32(valueToReturn.Substring(0, 1))); - int column = (Convert.ToInt32(valueToReturn.Substring(2, 1))); ; - if (isTaken(board, row, column)) - { - valueToReturn = "n"; - } - } - testBoard[i, i] = board[i, i]; - } - if (valueToReturn != "n") - { - testBoard[0, 2] = playerTurn; - if (checkDiagonal(testBoard) != "n") - { - valueToReturn = "0,2"; - int row = (Convert.ToInt32(valueToReturn.Substring(0, 1))); - int column = (Convert.ToInt32(valueToReturn.Substring(2, 1))); ; - if (isTaken(board, row, column)) - { - valueToReturn = "n"; - } - } - testBoard[0, 2] = board[0, 2]; - testBoard[1, 1] = playerTurn; - if (checkDiagonal(testBoard) != "n") - { - valueToReturn = "1,1"; - int row = (Convert.ToInt32(valueToReturn.Substring(0, 1))); - int column = (Convert.ToInt32(valueToReturn.Substring(2, 1))); ; - if (isTaken(board, row, column)) - { - valueToReturn = "n"; - } - } - testBoard[1, 1] = board[1, 1]; - testBoard[2, 0] = playerTurn; - if (checkDiagonal(testBoard) != "n") - { - valueToReturn = "2,0"; - int row = (Convert.ToInt32(valueToReturn.Substring(0, 1))); - int column = (Convert.ToInt32(valueToReturn.Substring(2, 1))); ; - if (isTaken(board, row, column)) - { - valueToReturn = "n"; - } - } - } - return valueToReturn; - } - } -} From c4f870a7ccb0be861529cad1d3c465420c463aba Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 23 Jan 2019 16:03:20 -0600 Subject: [PATCH 12/19] finished seperating the files --- keepScoreTicTT/Program.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/keepScoreTicTT/Program.cs b/keepScoreTicTT/Program.cs index bfd7de81..8a82e26e 100644 --- a/keepScoreTicTT/Program.cs +++ b/keepScoreTicTT/Program.cs @@ -7,9 +7,11 @@ class Program { public static void Main() { - string[] returnedResults = new string[1]; - mainMenu(returnedResults); + string[,][] returnedResults = new string[3, 3][]; + Menu.mainMenu(returnedResults); } + } +} From c346383ec1abf469086ab331331e05dbd2a6172d Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 25 Jan 2019 08:04:00 -0600 Subject: [PATCH 13/19] everything is seperated now working on getting it all together --- keepScoreTicTT/Menu.cs | 108 ++++++++++++++ keepScoreTicTT/PlayerAuto.cs | 255 +++++++++++++++++++++++++++++++ keepScoreTicTT/RunGame.cs | 281 +++++++++++++++++++++++++++++++++++ keepScoreTicTT/Utility.cs | 53 +++++++ 4 files changed, 697 insertions(+) create mode 100644 keepScoreTicTT/Menu.cs create mode 100644 keepScoreTicTT/PlayerAuto.cs create mode 100644 keepScoreTicTT/RunGame.cs create mode 100644 keepScoreTicTT/Utility.cs diff --git a/keepScoreTicTT/Menu.cs b/keepScoreTicTT/Menu.cs new file mode 100644 index 00000000..61b656a8 --- /dev/null +++ b/keepScoreTicTT/Menu.cs @@ -0,0 +1,108 @@ +using System; +using System.Text; + +namespace TicTacToe +{ + public class Menu + { + public static void mainMenu(string[,][] returnedResults) + { + int howMany = Convert.ToInt32(returnedResults[0, 0][0]); + Console.WriteLine("*************************************************"); + Console.WriteLine("* Main Menu *"); + Console.WriteLine("* *"); + Console.WriteLine("* Play Games: p View Games: v *"); + Console.WriteLine("* *"); + Console.WriteLine("*************************************************"); + string request = Console.ReadLine(); + string[] results = new string[howMany]; + + if (request == "p") + { + setupGame(); + } + else if (request == "v") + { + if (!string.IsNullOrEmpty(results[0])) + { + showResults(results); + } + } + + } + public static void showResults(string[] returnedResults) + { + string[] resultsToPrint = returnedResults; + + for (int i = 1; i >= Convert.ToInt32(resultsToPrint[0]); i++) + { + string resultString = resultsToPrint[i]; + + } + + } + public static string[,][] setupGame() + { + bool playerOneAI = false; + bool playerTwoAI = true; + int levelOne = 1; + int levelTwo = 1; + + Console.WriteLine("Is player 1 an AI y/n"); + string answer = Console.ReadLine(); + if (answer.Substring(0, 1).ToLower() == "y") + { + playerOneAI = true; + Console.WriteLine("Which difficulty level"); + Console.WriteLine("Easy: 0 | Medium: 1 | Hard: 2"); + int difficultyRequest = Convert.ToInt32(Console.ReadLine()); + if (difficultyRequest == 0 || Convert.ToInt32(difficultyRequest) == 1 || Convert.ToInt32(difficultyRequest) == 2) + { + levelOne = Convert.ToInt32(difficultyRequest); + } + + } + else + { + playerOneAI = false; + } + Console.WriteLine("Is player 2 an AI y/n"); + string answerTwo = Console.ReadLine(); + if (answer.Substring(0, 1).ToLower() == "y") + { + playerOneAI = true; + Console.WriteLine("Which difficulty level"); + Console.WriteLine("Easy: 0 | Medium: 1 | Hard: 2"); + int difficultyRequest = Convert.ToInt32(Console.ReadLine()); + if (difficultyRequest == 0 || Convert.ToInt32(difficultyRequest) == 1 || Convert.ToInt32(difficultyRequest) == 2) + { + levelTwo = Convert.ToInt32(difficultyRequest); + } + + } + else + { + playerOneAI = false; + } + Console.WriteLine("*************************************************"); + Console.WriteLine("* how many games would you like to play? *"); + Console.WriteLine("*************************************************"); + Console.WriteLine("* *"); + Console.WriteLine("* Enter a number: 1 and 500 *"); + Console.WriteLine("* *"); + Console.WriteLine("*************************************************"); + int gameAmount = (Convert.ToInt32(Console.ReadLine())); + string[,][] gameResults = new string[3, 3][]; + //string[,][] = new string[,][]; an array of arrays + // p = new + // ask about dynamic creating array lengths + for (int i = 0; i < gameAmount; i++) + { + string[,] gameStorage = new string[3, 3]; + gameStorage = runGame.playGame(0, 0, 0, "x", playerOneAI, playerTwoAI, levelOne, levelTwo); + gameResults[3, 3][i] = gameStorage; + } + return gameResults; + } + } +} diff --git a/keepScoreTicTT/PlayerAuto.cs b/keepScoreTicTT/PlayerAuto.cs new file mode 100644 index 00000000..3a203957 --- /dev/null +++ b/keepScoreTicTT/PlayerAuto.cs @@ -0,0 +1,255 @@ +using System; +using System.Text; + +namespace TicTacToe +{ + public class PlayerAuto + { + private static readonly Random getrandom = new Random(); + + public static string autoSelect(string[,] board, int level, string playerTurn) + { + + string computerGuess = "n"; + if (level == 0) + { + computerGuess = easyAI() + "," + easyAI(); + } + else if (level == 1) + { + computerGuess = mediumAI(board, playerTurn); + } + //return appropriate level AI's choice + return computerGuess; + } + + public static int easyAI() + { + int aiGuess = getrandom.Next(0, 31); + int guess = 0; + if (aiGuess <= 10) + { + guess = 0; + } + else if (aiGuess <= 20) + { + guess = 1; + } + else + { + guess = 2; + } + return guess; + } + + public static string mediumAI(string[,] board, string playerTurn) + { + string opponentTurn = (playerTurn == "X") ? "O" : "X"; + string checkForWin = lookForWin(board, playerTurn); + string checkForBlock = lookForWin(board, opponentTurn); + + if (checkForWin != "n") + { + return checkForWin; + } + else if (checkForBlock != "n") + { + return checkForBlock; + } + else + { + return easyAI() + "," + easyAI(); + } + } + + public static string lookForWin(string[,] board, string playerTurn) + { + string potentialRowVictory = checkRowpotential(board, playerTurn); + string potentialColumnVictory = checkColumnpotential(board, playerTurn); + string potentialDiagVictory = checkDiagPotential(board, playerTurn); + + string returnValue = "n"; + + //check each + if (potentialRowVictory != "n") + { + returnValue = potentialRowVictory; + } + else if (potentialColumnVictory != "n") + { + returnValue = potentialColumnVictory; + } + else if (potentialDiagVictory != "n") + { + returnValue = potentialDiagVictory; + } + return returnValue; + } + public static string checkRowpotential(string[,] board, string playerTurn) + { + string valueToReturn = "n"; + for (int i = 0; i < 3; i++) + { + if (String.IsNullOrWhiteSpace(board[i, 0])) + { + if (board[i, 1] == board[i, 2] && board[i, 1] == playerTurn) + { + valueToReturn = i + ",0"; + int row = (Convert.ToInt32(valueToReturn.Substring(0, 1))); + int column = (Convert.ToInt32(valueToReturn.Substring(2, 1))); ; + if (runGame.isTaken(board, row, column)) + { + valueToReturn = "n"; + } + } + } + else if (String.IsNullOrWhiteSpace(board[i, 1])) + { + if (board[i, 0] == board[i, 2] && board[i, 0] == playerTurn) + { + + valueToReturn = i + ",1"; + int row = (Convert.ToInt32(valueToReturn.Substring(0, 1))); + int column = (Convert.ToInt32(valueToReturn.Substring(2, 1))); ; + if (runGame.isTaken(board, row, column)) + { + valueToReturn = "n"; + } + } + } + else if (String.IsNullOrWhiteSpace(board[i, 2])) + { + if (board[i, 1] == board[i, 0] && board[i, 1] == playerTurn) + { + + valueToReturn = i + ",2"; + int row = (Convert.ToInt32(valueToReturn.Substring(0, 1))); + int column = (Convert.ToInt32(valueToReturn.Substring(2, 1))); ; + if (runGame.isTaken(board, row, column)) + { + valueToReturn = "n"; + } + } + } + else + { + valueToReturn = "n"; + + } + } + return valueToReturn; + } + public static string checkColumnpotential(string[,] board, string playerTurn) + { + string valueToReturn = "n"; ; + for (int i = 0; i < 3; i++) + { + if (String.IsNullOrWhiteSpace(board[0, i])) + { + if (board[1, i] == board[2, i] && board[1, i] == playerTurn) + { + valueToReturn = "0," + i; + int row = (Convert.ToInt32(valueToReturn.Substring(0, 1))); + int column = (Convert.ToInt32(valueToReturn.Substring(2, 1))); ; + if (runGame.isTaken(board, row, column)) + { + valueToReturn = "n"; + } + } + } + else if (String.IsNullOrWhiteSpace(board[1, i])) + { + if (board[0, i] == board[2, i] && board[0, i] == playerTurn) + { + + valueToReturn = "1," + i; + int row = (Convert.ToInt32(valueToReturn.Substring(0, 1))); + int column = (Convert.ToInt32(valueToReturn.Substring(2, 1))); ; + if (runGame.isTaken(board, row, column)) + { + valueToReturn = "n"; + } + } + } + else if (String.IsNullOrWhiteSpace(board[i, 2])) + { + if (board[i, 1] == board[i, 0] && board[i, 1] == playerTurn) + { + + valueToReturn = "2," + i; + int row = (Convert.ToInt32(valueToReturn.Substring(0, 1))); + int column = (Convert.ToInt32(valueToReturn.Substring(2, 1))); ; + if (runGame.isTaken(board, row, column)) + { + valueToReturn = "n"; + } + } + } + else + { + valueToReturn = "n"; + } + } + return valueToReturn; + } + public static string checkDiagPotential(string[,] board, string playerTurn) + { + string valueToReturn = "n"; ; + string[,] testBoard = board; + for (int i = 0; i > 2; i++) + { + testBoard[i, i] = playerTurn; + if (runGame.checkDiagonal(testBoard) != "n") + { + valueToReturn = i.ToString() + "," + i.ToString(); + int row = (Convert.ToInt32(valueToReturn.Substring(0, 1))); + int column = (Convert.ToInt32(valueToReturn.Substring(2, 1))); ; + if (runGame.isTaken(board, row, column)) + { + valueToReturn = "n"; + } + } + testBoard[i, i] = board[i, i]; + } + if (valueToReturn != "n") + { + testBoard[0, 2] = playerTurn; + if (runGame.checkDiagonal(testBoard) != "n") + { + valueToReturn = "0,2"; + int row = (Convert.ToInt32(valueToReturn.Substring(0, 1))); + int column = (Convert.ToInt32(valueToReturn.Substring(2, 1))); ; + if (runGame.isTaken(board, row, column)) + { + valueToReturn = "n"; + } + } + testBoard[0, 2] = board[0, 2]; + testBoard[1, 1] = playerTurn; + if (runGame.checkDiagonal(testBoard) != "n") + { + valueToReturn = "1,1"; + int row = (Convert.ToInt32(valueToReturn.Substring(0, 1))); + int column = (Convert.ToInt32(valueToReturn.Substring(2, 1))); ; + if (runGame.isTaken(board, row, column)) + { + valueToReturn = "n"; + } + } + testBoard[1, 1] = board[1, 1]; + testBoard[2, 0] = playerTurn; + if (runGame.checkDiagonal(testBoard) != "n") + { + valueToReturn = "2,0"; + int row = (Convert.ToInt32(valueToReturn.Substring(0, 1))); + int column = (Convert.ToInt32(valueToReturn.Substring(2, 1))); ; + if (runGame.isTaken(board, row, column)) + { + valueToReturn = "n"; + } + } + } + return valueToReturn; + } + } +} \ No newline at end of file diff --git a/keepScoreTicTT/RunGame.cs b/keepScoreTicTT/RunGame.cs new file mode 100644 index 00000000..41246605 --- /dev/null +++ b/keepScoreTicTT/RunGame.cs @@ -0,0 +1,281 @@ +using System; +using System.Text; + +namespace TicTacToe +{ + class runGame + { + + + // if not static need to create a new ie var = new playGame() + //if static ie program.playgame + public static string[,] playGame(int x, int o, int t, string previousWinner, bool playerOneAI, bool playerTwoAI, int levelOne, int levelTwo) + { + //keep track of wins and ties + int xWins = x; + int oWins = o; + int timesTied = t; + //gameplay conditions + bool win = false; + string playerTurn = "X"; + if (previousWinner == "X" || previousWinner == "O") + { + playerTurn = (previousWinner == "X") ? "X" : "O"; + } + string winner = "n"; + string tie = "n"; + string[,] board = new string[3, 3]; + + bool isOneAI = playerOneAI; + bool isTwoAI = playerTwoAI; + int playerOnelevel = levelOne; + int playerTwolevel = levelTwo; + bool playAgain = true; + //keeps track of the question of manual turns + bool marker = true; + //keeps track of a full turn rotation + int turnTicker = 0; + int turns = 1; + while (!win) + { + //Make the Board + printBoard(board); + + //determine if its an NPC or human, and collect the row and column choice. + int row; + int column; + if (isTwoAI && playerTurn == "O") + { + string selectedSpot = PlayerAuto.autoSelect(board, playerTwolevel, playerTurn); + + row = (Convert.ToInt32(selectedSpot.Substring(0, 1))); + column = (Convert.ToInt32(selectedSpot.Substring(2, 1))); ; + string computerGuess = (playerTurn + " guess is: " + row.ToString() + " , " + column.ToString()); + Console.WriteLine(); + Console.WriteLine(computerGuess); + Console.WriteLine(); + } + else if (isOneAI && playerTurn == "X") + { + string selectedSpot = PlayerAuto.autoSelect(board, playerOnelevel, playerTurn); + + row = (Convert.ToInt32(selectedSpot.Substring(0, 1))); + column = (Convert.ToInt32(selectedSpot.Substring(2, 1))); ; + string computerGuess = (playerTurn + " guess is: " + row.ToString() + " , " + column.ToString()); + Console.WriteLine(); + Console.WriteLine(computerGuess); + Console.WriteLine(); + } + else + { + row = manualSelect(playerTurn, marker); + marker = false; + column = manualSelect(playerTurn, marker); + marker = true; + } + + //check if that spot is taken + if (isTaken(board, row, column)) + { + Console.WriteLine("Sorry that spot is taken"); + } + else + { + board[row, column] = playerTurn; + playerTurn = (playerTurn == "X") ? "O" : "X"; + turnTicker++; + if ((turnTicker % 2) == 0) + { + turns++; + } + } + + //check for win + if (turns >= 3) + { + tie = checkTie(board); + winner = checkWin(board); + } + + if (winner == "X") + { + xWins++; + Console.Clear(); + printBoard(board); + Console.WriteLine("After {0} turns X is victorious!", turns); + Console.WriteLine(); + Console.WriteLine("There have been {0} Ties the score is", timesTied); + Console.WriteLine("X: {0}", xWins); + Console.WriteLine("O: {0}", oWins); + System.Threading.Thread.Sleep(10000); + win = true; + } + else if (winner == "O") + { + oWins++; + Console.Clear(); + printBoard(board); + Console.WriteLine("After {0} turns O is victorious!", turns); + Console.WriteLine(); + Console.WriteLine("There have been {0} Ties the score is", timesTied); + Console.WriteLine("X: {0}", xWins); + Console.WriteLine("O: {0}", oWins); + System.Threading.Thread.Sleep(10000); + win = true; + } + else if (tie == "T") + { + timesTied++; + Console.Clear(); + printBoard(board); + Console.WriteLine("After {0} turns We have a Tie!", turns); + Console.WriteLine("There have been {0} Ties the score is", timesTied); + Console.WriteLine("X: {0}", xWins); + Console.WriteLine("O: {0}", oWins); + System.Threading.Thread.Sleep(10000); + win = true; + } + + + } + + return board; + + } + public static bool isTaken(string[,] board, int row, int column)//checks to see if a given tic tac toe spot is occupied + { + if (!String.IsNullOrEmpty(board[row, column]) || !String.IsNullOrWhiteSpace(board[row, column])) + { + return true; + } + else + { + return false; + } + } + + public static string checkWin(string[,] board) + { + string checkRows = checkHorizontal(board); + string checkColumns = checkVertical(board); + string checkCorner2Corner = checkDiagonal(board); + + if (checkRows == "X" || checkColumns == "X" || checkCorner2Corner == "X") + { + return "X"; + } + else if (checkRows == "O" || checkColumns == "O" || checkCorner2Corner == "O") + { + return "O"; + } + else + { + return "n"; + } + } + public static string checkHorizontal(string[,] board) + { + string isWinner = "n"; + for (int i = 0; i <= 2; i++) + { + if (!String.IsNullOrWhiteSpace(board[i, 0]) && !String.IsNullOrEmpty(board[i, 0])) + { + if (board[i, 0] == board[i, 1] && board[i, 0] == board[i, 2]) + { + isWinner = board[i, 0]; + } + } + else + { + isWinner = "n"; + } + } + Console.WriteLine(isWinner); + return isWinner; + } + + public static string checkVertical(string[,] board) + { + string isWinner = "n"; + for (int i = 0; i <= 2; i++) + { + if (board[0, i] == board[1, i] && board[0, i] == board[2, i] && !String.IsNullOrEmpty(board[0, i]) && !String.IsNullOrWhiteSpace(board[0, 0])) + { + isWinner = board[0, i]; + } + } + return isWinner; + } + + public static string checkDiagonal(string[,] board) + { + string isWinner = "n"; + if (board[0, 0] == board[1, 1] && board[0, 0] == board[2, 2] && !String.IsNullOrEmpty(board[0, 0]) && !String.IsNullOrWhiteSpace(board[0, 0])) + { + isWinner = board[0, 0]; + } + else if (board[0, 2] == board[1, 1] && board[0, 2] == board[2, 0] && !String.IsNullOrEmpty(board[0, 2]) && !String.IsNullOrWhiteSpace(board[0, 2])) + { + isWinner = board[0, 2]; + } + return isWinner; + } + + public static string checkTie(string[,] board) + { + string isTie = "n"; + for (int i = 0; i <= 2; i++) + { + if (String.IsNullOrWhiteSpace(board[i, 0]) || String.IsNullOrWhiteSpace(board[i, 1]) || String.IsNullOrWhiteSpace(board[i, 2])) + { + if (String.IsNullOrEmpty(board[i, 0]) || String.IsNullOrEmpty(board[i, 1]) || String.IsNullOrEmpty(board[i, 2])) + { + isTie = "n"; + break; + } + } + else + { + isTie = "T"; + } + } + return isTie; + } + + public static void printBoard(string[,] board) + { + Console.WriteLine(" " + "0" + " 1 " + "2"); + for (int i = 0; i <= 2; i++) + { + if (i != 2) + { + + Console.WriteLine(i + " " + Utility.unlessNull(board[i, 0]) + "|" + Utility.unlessNull(board[i, 1]) + "|" + Utility.unlessNull(board[i, 2])); + Console.WriteLine(" " + "-----"); + } + else + { + Console.WriteLine(i + " " + Utility.unlessNull(board[i, 0]) + "|" + Utility.unlessNull(board[i, 1]) + "|" + Utility.unlessNull(board[i, 2])); + } + } + } + + + public static int manualSelect(string player, bool marker) + { + int selection; + if (marker) + { + Console.WriteLine("player {0}'s turn!", player); + Console.Write("Enter Row: "); + selection = Int32.Parse(Console.ReadLine()); + } + else + { + Console.Write("Enter Column: "); + selection = Int32.Parse(Console.ReadLine()); + } + return selection; + } + } +} \ No newline at end of file diff --git a/keepScoreTicTT/Utility.cs b/keepScoreTicTT/Utility.cs new file mode 100644 index 00000000..affa48ac --- /dev/null +++ b/keepScoreTicTT/Utility.cs @@ -0,0 +1,53 @@ +using System; +using System.Text; + +namespace TicTacToe +{ + public class Utility + { + public static string boardToString(string[,] board) + { + string[,] winningBoard = board; + var sb = new StringBuilder(string.Empty); + for (var i = 0; i < 3; i++) + { + for (var j = 0; j < 3; j++) + { + if (!string.IsNullOrWhiteSpace(winningBoard[i, j])) + { + if (j != 2) + { + sb.Append(winningBoard[i, j] + ","); + } + else + { + sb.Append(winningBoard[i, j]); + } + } + else + { + sb.Append("n" + ","); + } + } + if (i != 2) + { + sb.Append("*"); + } + } + var result = sb.ToString(); + return result; + } + public static string unlessNull(string checkValue) + { + if (string.IsNullOrEmpty(checkValue)) + { + return " "; + } + else + { + return checkValue; + } + } + + } +} \ No newline at end of file From 5b8ff8e29930ae7d47ed7a0e1450868d9d567c9e Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 26 Jan 2019 13:41:12 -0600 Subject: [PATCH 14/19] moving to sorting --- keepScoreTicTT/Menu.cs | 16 +++++++++------- keepScoreTicTT/Program.cs | 3 ++- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/keepScoreTicTT/Menu.cs b/keepScoreTicTT/Menu.cs index 61b656a8..27a79e04 100644 --- a/keepScoreTicTT/Menu.cs +++ b/keepScoreTicTT/Menu.cs @@ -5,9 +5,9 @@ namespace TicTacToe { public class Menu { - public static void mainMenu(string[,][] returnedResults) + public static void mainMenu(string[][,] returnedResults) { - int howMany = Convert.ToInt32(returnedResults[0, 0][0]); + int howMany = Convert.ToInt32(returnedResults[0][0, 0]); Console.WriteLine("*************************************************"); Console.WriteLine("* Main Menu *"); Console.WriteLine("* *"); @@ -41,7 +41,7 @@ public static void showResults(string[] returnedResults) } } - public static string[,][] setupGame() + public static string[][,] setupGame() { bool playerOneAI = false; bool playerTwoAI = true; @@ -91,16 +91,18 @@ public static void showResults(string[] returnedResults) Console.WriteLine("* Enter a number: 1 and 500 *"); Console.WriteLine("* *"); Console.WriteLine("*************************************************"); + int gameAmount = (Convert.ToInt32(Console.ReadLine())); - string[,][] gameResults = new string[3, 3][]; + + string[][,] gameResults = new string[gameAmount][,]; //string[,][] = new string[,][]; an array of arrays // p = new // ask about dynamic creating array lengths - for (int i = 0; i < gameAmount; i++) + for (int i = 1; i == gameAmount; i++) { string[,] gameStorage = new string[3, 3]; - gameStorage = runGame.playGame(0, 0, 0, "x", playerOneAI, playerTwoAI, levelOne, levelTwo); - gameResults[3, 3][i] = gameStorage; + gameResults[i] = runGame.playGame(0, 0, 0, "x", playerOneAI, playerTwoAI, levelOne, levelTwo); + } return gameResults; } diff --git a/keepScoreTicTT/Program.cs b/keepScoreTicTT/Program.cs index 8a82e26e..c6f04f2d 100644 --- a/keepScoreTicTT/Program.cs +++ b/keepScoreTicTT/Program.cs @@ -7,7 +7,8 @@ class Program { public static void Main() { - string[,][] returnedResults = new string[3, 3][]; + string[][,] returnedResults = new string[1][,]; + Menu.mainMenu(returnedResults); } } From e98ad4c6c0a9471d4af677c536febea96fa98b55 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 28 Jan 2019 16:56:11 -0600 Subject: [PATCH 15/19] finished tie function need to print out winner --- TicTacToe/TicTacToe.cs | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/TicTacToe/TicTacToe.cs b/TicTacToe/TicTacToe.cs index a1bfe6e0..0eb91a3c 100644 --- a/TicTacToe/TicTacToe.cs +++ b/TicTacToe/TicTacToe.cs @@ -56,8 +56,6 @@ public static void Main() break; } } - - return; } } @@ -137,13 +135,28 @@ public static string checkDiagonal(string[,] board) public static string checkTie(string[,] board) { + string winner = checkWin(board); + if (winner == "n") + { + for (int i = 0; i < 8; i++) + { + if (String.IsNullOrEmpty(board[i, 0])) + { + return "n"; + } + else if (String.IsNullOrEmpty(board[i, 1])) + { + return "n"; + } + else if (String.IsNullOrEmpty(board[i, 2])) + { + return "n"; + } + } + return "t"; + } return "n"; } - - public static void showBoard(string[,] board) - { - return; - } public static void printBoard(string[,] board) { Console.WriteLine(" " + "0" + " 1 " + "2"); From 74d693f0a12005a01fadbe04760175c5281dba25 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 28 Jan 2019 17:20:53 -0600 Subject: [PATCH 16/19] found issues all over the code ha ha this was done a couple weeks ago and all time ater that was spent on the keeping score version fixing errors --- TicTacToe/TicTacToe.cs | 74 +++++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 41 deletions(-) diff --git a/TicTacToe/TicTacToe.cs b/TicTacToe/TicTacToe.cs index 0eb91a3c..d9a83243 100644 --- a/TicTacToe/TicTacToe.cs +++ b/TicTacToe/TicTacToe.cs @@ -6,56 +6,48 @@ class Program { public static void Main() { + string playerTurn = "X"; + int turn = 0; + string[,] board = new string[3, 3]; bool win = false; while (!win) { - string playerTurn = "X"; - int turn = 0; - string[,] board = new string[3, 3]; - - for (int i = 1; i <= 9; i++) + printBoard(board); + Console.WriteLine("Player " + playerTurn); + Console.WriteLine("Enter Row:"); + int row = Int32.Parse(Console.ReadLine()); + Console.WriteLine("Enter Column:"); + int column = Int32.Parse(Console.ReadLine()); + Console.Clear(); + //check if that spot is taken + if (isTaken(board, row, column)) { - printBoard(board); - Console.WriteLine("Player " + playerTurn); - Console.WriteLine("Enter Row:"); - int row = Int32.Parse(Console.ReadLine()); - Console.WriteLine("Enter Column:"); - int column = Int32.Parse(Console.ReadLine()); - Console.Clear(); - //check if that spot is taken - if (isTaken(board, row, column)) - { - Console.WriteLine("Sorry that spot is taken"); - } - else - { - board[row, column] = playerTurn; - playerTurn = (playerTurn == "X") ? "O" : "X"; - } + Console.WriteLine("Sorry that spot is taken"); + } + else + { + board[row, column] = playerTurn; + playerTurn = (playerTurn == "X") ? "O" : "X"; + turn++; + } - if ((2 % i) == 0) - { - turn++; - } + string winner = "n"; + winner = checkWin(board); + winner = checkTie(board); - string winner = "n"; - if (i >= 3) - { - Console.WriteLine("checking winner"); - winner = checkWin(board); - } - if (i >= 7) - { - winner = checkTie(board); - } - if (winner == "X" || winner == "O") - { + if (winner == "X" || winner == "O") + { + Console.WriteLine("The winner is {0}", winner); + win = true; + } + else if (winner == "T") + { + Console.WriteLine("Its a Tie!") win = true; - break; - } } + } } @@ -155,7 +147,7 @@ public static string checkTie(string[,] board) } return "t"; } - return "n"; + return winner; } public static void printBoard(string[,] board) { From 3e513f5b85637f843402f1888a732956e4007593 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 28 Jan 2019 17:53:37 -0600 Subject: [PATCH 17/19] basicly rewrighting the whole thing 40 min before its due --- TicTacToe/TicTacToe.cs | 76 ++++++++++++++++++++++++++---------------- 1 file changed, 47 insertions(+), 29 deletions(-) diff --git a/TicTacToe/TicTacToe.cs b/TicTacToe/TicTacToe.cs index d9a83243..3b4e7f64 100644 --- a/TicTacToe/TicTacToe.cs +++ b/TicTacToe/TicTacToe.cs @@ -5,38 +5,21 @@ namespace TicTacToe class Program { public static void Main() + { + playGame(); + } + public static void playGame() { string playerTurn = "X"; - int turn = 0; + string winner = "n"; string[,] board = new string[3, 3]; bool win = false; while (!win) { - printBoard(board); - Console.WriteLine("Player " + playerTurn); - Console.WriteLine("Enter Row:"); - int row = Int32.Parse(Console.ReadLine()); - Console.WriteLine("Enter Column:"); - int column = Int32.Parse(Console.ReadLine()); - Console.Clear(); - //check if that spot is taken - if (isTaken(board, row, column)) - { - Console.WriteLine("Sorry that spot is taken"); - } - else - { - board[row, column] = playerTurn; - playerTurn = (playerTurn == "X") ? "O" : "X"; - turn++; - } - - string winner = "n"; + board = selectSpot(board, playerTurn); + playerTurn = (playerTurn == "X") ? "O" : "X"; winner = checkWin(board); winner = checkTie(board); - - - if (winner == "X" || winner == "O") { Console.WriteLine("The winner is {0}", winner); @@ -44,23 +27,58 @@ public static void Main() } else if (winner == "T") { - Console.WriteLine("Its a Tie!") - win = true; + Console.WriteLine("Its a Tie!"); + win = true; + } + } + } + + public static string[,] selectSpot(string[,] board, string playerTurn) + { + int row = 10; + int column = 10; + bool controller = true; + while (controller) + { + printBoard(board); + Console.WriteLine("Player " + playerTurn); + Console.WriteLine("Enter Row:"); + row = Int32.Parse(Console.ReadLine()); + Console.WriteLine("Enter Column:"); + column = Int32.Parse(Console.ReadLine()); + + if (isTaken(board, row, column)) + { + Console.WriteLine("sorry that spot is taken"); + } + else + { + controller = false; } } + board[row, column] = playerTurn; + return board; + } public static bool isTaken(string[,] board, int row, int column) { - if (String.IsNullOrEmpty(board[row, column])) + if (row != 10) { - return false; + if (String.IsNullOrEmpty(board[row, column])) + { + return false; + } + else + { + return true; + } } else { - return true; + return false; } } From 6b346a4ccda20ca467fe52522e97afca5b2827a6 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 28 Jan 2019 18:21:27 -0600 Subject: [PATCH 18/19] its working at least --- TicTacToe/TicTacToe.cs | 51 +++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/TicTacToe/TicTacToe.cs b/TicTacToe/TicTacToe.cs index 3b4e7f64..642ffa3e 100644 --- a/TicTacToe/TicTacToe.cs +++ b/TicTacToe/TicTacToe.cs @@ -11,25 +11,33 @@ public static void Main() public static void playGame() { string playerTurn = "X"; - string winner = "n"; + string isWinner = "n"; string[,] board = new string[3, 3]; bool win = false; while (!win) { board = selectSpot(board, playerTurn); playerTurn = (playerTurn == "X") ? "O" : "X"; - winner = checkWin(board); - winner = checkTie(board); - if (winner == "X" || winner == "O") - { - Console.WriteLine("The winner is {0}", winner); - win = true; - } - else if (winner == "T") - { - Console.WriteLine("Its a Tie!"); - win = true; - } + isWinner = checkWin(board); + isWinner = checkTie(board); + win = checkResult(isWinner); + } + } + public static bool checkResult(string winner) + { + if (winner == "X" || winner == "O") + { + Console.WriteLine("The winner is {0}", winner); + return true; + } + else if (winner == "T") + { + Console.WriteLine("Its a Tie!"); + return true; + } + else + { + return false; } } @@ -65,7 +73,7 @@ public static void playGame() public static bool isTaken(string[,] board, int row, int column) { - if (row != 10) + if (row != 10 || column != 10) { if (String.IsNullOrEmpty(board[row, column])) { @@ -100,6 +108,7 @@ public static string checkWin(string[,] board) return "n"; } } + public static string checkHorizontal(string[,] board) { string isWinner = "n"; @@ -110,9 +119,7 @@ public static string checkHorizontal(string[,] board) isWinner = board[i, 0]; } } - Console.WriteLine(isWinner); return isWinner; - } public static string checkVertical(string[,] board) @@ -148,17 +155,9 @@ public static string checkTie(string[,] board) string winner = checkWin(board); if (winner == "n") { - for (int i = 0; i < 8; i++) + for (int i = 0; i < 3; i++) { - if (String.IsNullOrEmpty(board[i, 0])) - { - return "n"; - } - else if (String.IsNullOrEmpty(board[i, 1])) - { - return "n"; - } - else if (String.IsNullOrEmpty(board[i, 2])) + if (String.IsNullOrEmpty(board[i, 0]) || String.IsNullOrEmpty(board[i, 1]) || String.IsNullOrEmpty(board[i, 2])) { return "n"; } From 4a1694659917c0e4e44077e4a147c8d4f191df80 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 5 Feb 2019 21:03:06 -0600 Subject: [PATCH 19/19] not sure why its skipping the loop --- keepScoreTicTT/Menu.cs | 9 ++++----- keepScoreTicTT/Program.cs | 4 ++-- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/keepScoreTicTT/Menu.cs b/keepScoreTicTT/Menu.cs index 27a79e04..9d451e7c 100644 --- a/keepScoreTicTT/Menu.cs +++ b/keepScoreTicTT/Menu.cs @@ -5,9 +5,9 @@ namespace TicTacToe { public class Menu { - public static void mainMenu(string[][,] returnedResults) + public static void mainMenu(string[][,] returnedResults, string how_many) { - int howMany = Convert.ToInt32(returnedResults[0][0, 0]); + int howMany = Convert.ToInt32(how_many); Console.WriteLine("*************************************************"); Console.WriteLine("* Main Menu *"); Console.WriteLine("* *"); @@ -92,13 +92,12 @@ public static void showResults(string[] returnedResults) Console.WriteLine("* *"); Console.WriteLine("*************************************************"); - int gameAmount = (Convert.ToInt32(Console.ReadLine())); + int gameAmount = 5;//Convert.ToInt32(Console.ReadLine()); string[][,] gameResults = new string[gameAmount][,]; //string[,][] = new string[,][]; an array of arrays // p = new - // ask about dynamic creating array lengths - for (int i = 1; i == gameAmount; i++) + for (int i = 0; i == gameAmount; i++) { string[,] gameStorage = new string[3, 3]; gameResults[i] = runGame.playGame(0, 0, 0, "x", playerOneAI, playerTwoAI, levelOne, levelTwo); diff --git a/keepScoreTicTT/Program.cs b/keepScoreTicTT/Program.cs index c6f04f2d..050f8b27 100644 --- a/keepScoreTicTT/Program.cs +++ b/keepScoreTicTT/Program.cs @@ -8,8 +8,8 @@ class Program public static void Main() { string[][,] returnedResults = new string[1][,]; - - Menu.mainMenu(returnedResults); + string how_many = "5"; + Menu.mainMenu(returnedResults, how_many); } } }