diff --git a/HelloWorld/HelloWorld.cs b/HelloWorld/HelloWorld.cs index d25a8c44..3a382d3c 100644 --- a/HelloWorld/HelloWorld.cs +++ b/HelloWorld/HelloWorld.cs @@ -12,4 +12,5 @@ static void Main(string[] args) Console.Write(yourName); } } + } diff --git a/TicTacToe/.vscode/launch.json b/TicTacToe/.vscode/launch.json new file mode 100644 index 00000000..b35dd83d --- /dev/null +++ b/TicTacToe/.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.0/TicTacToe.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": "internalConsole", + "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..aa4b20aa 100644 --- a/TicTacToe/TicTacToe.cs +++ b/TicTacToe/TicTacToe.cs @@ -4,7 +4,7 @@ namespace TicTacToe { class Program { - public static string playerTurn = "X"; + public static string playerTurn = "O"; public static string[][] board = new string[][] { new string[] {" ", " ", " "}, @@ -16,11 +16,20 @@ public static void Main() { do { + playerTurn = (playerTurn == "X") ? "O" : "X"; ; DrawBoard(); GetInput(); } while (!CheckForWin() && !CheckForTie()); - + DrawBoard(); + if (CheckForWin()) + { + System.Console.WriteLine(playerTurn + " wins!"); + } + else + { + System.Console.WriteLine("It's a tie!"); + } // leave this command at the end so your program does not close automatically Console.ReadLine(); } @@ -32,46 +41,53 @@ public static void GetInput() int row = int.Parse(Console.ReadLine()); Console.WriteLine("Enter Column:"); int column = int.Parse(Console.ReadLine()); + PlaceMark(row, column); } public static void PlaceMark(int row, int column) { - // your code goes here + // your code goes here + board[row][column] = playerTurn; } public static bool CheckForWin() { - // your code goes here - - return false; + return (HorizontalWin() || VerticalWin() || DiagonalWin()); } public static bool CheckForTie() { // your code goes here - - return false; + return (!CheckForWin() && board[0][0] != " " && board[0][1] != " " && board[0][2] != " " + && board[1][0] != " " && board[1][1] != " " && board[1][2] != " " + && board[2][0] != " " && board[2][1] != " " && board[2][2] != " "); } - + public static bool HorizontalWin() { - // your code goes here + // your code goes here + bool row1 = (board[0][0] == playerTurn && board[0][1] == playerTurn && board[0][2] == playerTurn); + bool row2 = (board[1][0] == playerTurn && board[1][1] == playerTurn && board[1][2] == playerTurn); + bool row3 = (board[2][0] == playerTurn && board[2][1] == playerTurn && board[2][2] == playerTurn); - return false; + return (row1 || row2 || row3); } public static bool VerticalWin() { // your code goes here - - return false; + bool column1 = (board[0][0] == playerTurn && board[1][0] == playerTurn && board[2][0] == playerTurn); + bool column2 = (board[0][1] == playerTurn && board[1][1] == playerTurn && board[2][1] == playerTurn); + bool column3 = (board[0][2] == playerTurn && board[1][2] == playerTurn && board[2][2] == playerTurn); + return (column1 || column2 || column3); } public static bool DiagonalWin() { // your code goes here - - return false; + bool diagonal1 = (board[0][0] == playerTurn && board[1][1] == playerTurn && board[2][2] == playerTurn); + bool diagonal2 = (board[0][2] == playerTurn && board[1][1] == playerTurn && board[2][0] == playerTurn); + return (diagonal1 || diagonal2); } public static void DrawBoard()