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 c5751176..642ffa3e 100644 --- a/TicTacToe/TicTacToe.cs +++ b/TicTacToe/TicTacToe.cs @@ -4,84 +4,176 @@ 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 - { - DrawBoard(); - GetInput(); - - } while (!CheckForWin() && !CheckForTie()); - - // leave this command at the end so your program does not close automatically - Console.ReadLine(); + playGame(); } - - public static void GetInput() + public static void playGame() { - Console.WriteLine("Player " + playerTurn); - Console.WriteLine("Enter Row:"); - int row = int.Parse(Console.ReadLine()); - Console.WriteLine("Enter Column:"); - int column = int.Parse(Console.ReadLine()); + string playerTurn = "X"; + string isWinner = "n"; + string[,] board = new string[3, 3]; + bool win = false; + while (!win) + { + board = selectSpot(board, playerTurn); + playerTurn = (playerTurn == "X") ? "O" : "X"; + isWinner = checkWin(board); + isWinner = checkTie(board); + win = checkResult(isWinner); + } } - - public static void PlaceMark(int row, int column) + public static bool checkResult(string winner) { - // your code goes here + 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; + } } - public static bool CheckForWin() + public static string[,] selectSpot(string[,] board, string playerTurn) { - // your code goes here + 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; - return false; } - public static bool CheckForTie() + public static bool isTaken(string[,] board, int row, int column) { - // your code goes here - - return false; + if (row != 10 || column != 10) + { + if (String.IsNullOrEmpty(board[row, column])) + { + return false; + } + else + { + return true; + } + } + else + { + return false; + } } - - public static bool HorizontalWin() + + 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"; + } + } - return false; + 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]; + } + } + return isWinner; } - public static bool VerticalWin() + 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 DiagonalWin() + public static string checkDiagonal(string[,] board) { - // your code goes here - - return false; + 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 void DrawBoard() + public static string checkTie(string[,] board) + { + string winner = checkWin(board); + if (winner == "n") + { + for (int i = 0; i < 3; i++) + { + if (String.IsNullOrEmpty(board[i, 0]) || String.IsNullOrEmpty(board[i, 1]) || String.IsNullOrEmpty(board[i, 2])) + { + return "n"; + } + } + return "t"; + } + return winner; + } + 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]); } } } 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/Menu.cs b/keepScoreTicTT/Menu.cs new file mode 100644 index 00000000..9d451e7c --- /dev/null +++ b/keepScoreTicTT/Menu.cs @@ -0,0 +1,109 @@ +using System; +using System.Text; + +namespace TicTacToe +{ + public class Menu + { + public static void mainMenu(string[][,] returnedResults, string how_many) + { + int howMany = Convert.ToInt32(how_many); + 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 = 5;//Convert.ToInt32(Console.ReadLine()); + + string[][,] gameResults = new string[gameAmount][,]; + //string[,][] = new string[,][]; an array of arrays + // p = new + 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); + + } + 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/Program.cs b/keepScoreTicTT/Program.cs new file mode 100644 index 00000000..050f8b27 --- /dev/null +++ b/keepScoreTicTT/Program.cs @@ -0,0 +1,23 @@ +using System; +using System.Text; + +namespace TicTacToe +{ + class Program + { + public static void Main() + { + string[][,] returnedResults = new string[1][,]; + string how_many = "5"; + Menu.mainMenu(returnedResults, how_many); + } + } +} + + + + + + + + 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 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 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 + + +