-
Notifications
You must be signed in to change notification settings - Fork 0
Tictactoe #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Tictactoe #6
Changes from all commits
fb31233
4d645f0
1e25549
9e92332
467d137
620bc6c
5c5fab2
81b8a89
5c1bb8d
6b71ec3
0574968
c4f870a
c346383
5b8ff8e
e98ad4c
74d693f
3e513f5
6b346a4
4a16946
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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}" | ||
| } | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| { | ||
| "version": "2.0.0", | ||
| "tasks": [ | ||
| { | ||
| "label": "build", | ||
| "command": "dotnet", | ||
| "type": "process", | ||
| "args": [ | ||
| "build", | ||
| "${workspaceFolder}/TicTacToe.csproj" | ||
| ], | ||
| "problemMatcher": "$msCompile" | ||
| } | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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)) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only thing missing from this selectSpot method is making sure I don't enter values outside of [0,2] |
||
| { | ||
| 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) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. magic values are fine for now, but try to avoid them. Imagine looking at this method in 6 months. Will you remember what a row==10 value means?
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i dont remember what it means now .... good point. |
||
| { | ||
| 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])) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. clever solution |
||
| { | ||
| 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") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have a feeling you will really like |
||
| { | ||
| 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"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. your board formatting is not as good as the original. Do you know what you are doing different from the original printBoard method that is causing you board to squish up?
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the original. I fixed this in the Keep Score one and forgot to bring those changes back. |
||
| 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]); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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}" | ||
| }, | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| { | ||
| "version": "2.0.0", | ||
| "tasks": [ | ||
| { | ||
| "label": "build", | ||
| "command": "dotnet", | ||
| "type": "process", | ||
| "args": [ | ||
| "build", | ||
| "${workspaceFolder}/keepScoreTicTT.csproj" | ||
| ], | ||
| "problemMatcher": "$msCompile" | ||
| } | ||
| ] | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why did you name this
controller?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would "quit" and then "while(!quit)" be better that's what Ive been doing lately?