forked from AustinCodingAcademy/csharp-workbook
-
Notifications
You must be signed in to change notification settings - Fork 0
Created TicTacToe game #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
Open
mtrich
wants to merge
1
commit into
master
Choose a base branch
from
week3-1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,84 +4,158 @@ namespace TicTacToe | |
| { | ||
| class Program | ||
| { | ||
| //variable that determins who's turn it is | ||
| public static string playerTurn = "X"; | ||
|
|
||
| //Array that keeps track of places on board | ||
| public static string[][] board = new string[][] | ||
| { | ||
| new string[] {" ", " ", " "}, | ||
| new string[] {" ", " ", " "}, | ||
| new string[] {" ", " ", " "} | ||
| }; | ||
|
|
||
| public static void Main() | ||
| { | ||
| //draws board and runs game while there is no winner or tie | ||
| do | ||
| { | ||
| DrawBoard(); | ||
| GetInput(); | ||
|
|
||
| } while (!CheckForWin() && !CheckForTie()); | ||
|
|
||
| // leave this command at the end so your program does not close automatically | ||
| } while (!CheckForWin()&&!CheckForTie()); | ||
| // keeps program from closing automaticly | ||
| Console.ReadLine(); | ||
| } | ||
|
|
||
| //gets players move in terms of row and column number | ||
| public static void GetInput() | ||
| { | ||
| Console.WriteLine("Player " + playerTurn); | ||
| Console.WriteLine("Enter Row:"); | ||
| int row = int.Parse(Console.ReadLine()); | ||
| Console.WriteLine("Enter Column:"); | ||
| int column = int.Parse(Console.ReadLine()); | ||
| } | ||
|
|
||
| int column = Convert.ToInt16(Console.ReadLine()) - 1; | ||
| Console.WriteLine("Enter Row:"); | ||
| int row = Convert.ToInt16(Console.ReadLine()) - 1; | ||
| PlaceMark(row,column); | ||
| } | ||
| public static void PlaceMark(int row, int column) | ||
| { | ||
| // your code goes here | ||
| // checks if players movement choice is emptey | ||
| if (board[row][column] != "X" && board[row][column] != "O") | ||
| { | ||
| if (playerTurn == "X") | ||
|
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. Lots of repeated code. |
||
| { | ||
| //if game is not over, places x and switches to o | ||
| board[row][column] = "X"; | ||
| if(!CheckForWin() && !CheckForTie()) | ||
| { | ||
| playerTurn = "O"; | ||
| } | ||
| //if game is won, draws final board and announces winner | ||
| else if(CheckForWin()) | ||
| { | ||
| DrawBoard(); | ||
| Console.WriteLine(playerTurn + " Wins!"); | ||
| } | ||
| //if game is tie, draws final board and announces Tie | ||
| else if(CheckForTie()) | ||
| { | ||
| DrawBoard(); | ||
| Console.WriteLine("It's a Tie!"); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| //if game is not over, places 0 and switches to x | ||
| board[row][column] = "O"; | ||
| if(!CheckForWin() && !CheckForTie()) | ||
| { | ||
| playerTurn = "X"; | ||
| } | ||
| //if game is won, draws final board and announces winner | ||
| else if(CheckForWin()) | ||
| { | ||
| DrawBoard(); | ||
| Console.WriteLine(playerTurn + " Wins!"); | ||
| } | ||
| //if game is tie, draws final board and announces Tie | ||
| else if(CheckForTie()) | ||
| { | ||
| DrawBoard(); | ||
| Console.WriteLine("It's a Tie!"); | ||
| } | ||
| } | ||
| } | ||
| //if spot is taken anounces that the spot is taken | ||
| else | ||
| { | ||
| Console.WriteLine("Spot is already taken"); | ||
| } | ||
| } | ||
|
|
||
| //decides if game is won | ||
| public static bool CheckForWin() | ||
| { | ||
| // your code goes here | ||
|
|
||
| if(HorizontalWin()||VerticalWin()||DiagonalWin()) | ||
| { | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| public static bool CheckForTie() | ||
| { | ||
| // your code goes here | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| //checks for horizontal win | ||
| public static bool HorizontalWin() | ||
| { | ||
| // your code goes here | ||
|
|
||
| return false; | ||
| if ((board[0][0] == playerTurn && board[0][1] == playerTurn && board[0][2] == playerTurn)|| | ||
| (board[1][0] == playerTurn && board[1][1] == playerTurn && board[1][2] == playerTurn)|| | ||
| (board[2][0] == playerTurn && board[2][1] == playerTurn && board[2][2] == playerTurn)) | ||
| { | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| //checks for vertical win | ||
| public static bool VerticalWin() | ||
| { | ||
| // your code goes here | ||
|
|
||
| if ((board[0][0] == playerTurn && board[1][0] == playerTurn && board[2][0] == playerTurn)|| | ||
| (board[0][1] == playerTurn && board[1][1] == playerTurn && board[2][1] == playerTurn)|| | ||
| (board[0][2] == playerTurn && board[1][2] == playerTurn && board[2][2] == playerTurn)) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| //checks for diagonal win | ||
| public static bool DiagonalWin() | ||
| { | ||
| // your code goes here | ||
| if ((board[0][0] == playerTurn && board[1][1] == playerTurn && board[2][2] == playerTurn)|| | ||
| (board[2][0] == playerTurn && board[1][1] == playerTurn && board[0][2] == playerTurn)) | ||
| { | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| //checks for tie | ||
| public static bool CheckForTie() | ||
| { | ||
| if((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] != " ")&&(!HorizontalWin()||!VerticalWin()||!DiagonalWin())) | ||
| { | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| //draws board | ||
| public static void DrawBoard() | ||
| { | ||
| Console.WriteLine(" 0 1 2"); | ||
| Console.WriteLine("0 " + String.Join("|", board[0])); | ||
| Console.WriteLine(" 1 2 3"); | ||
| Console.WriteLine("1 " + "{0}|{1}|{2}",board[0][0],board[0][1],board[0][2]); | ||
| Console.WriteLine(" -----"); | ||
| Console.WriteLine("1 " + String.Join("|", board[1])); | ||
| Console.WriteLine("2 " + String.Join("|", board[1])); | ||
| Console.WriteLine(" -----"); | ||
| Console.WriteLine("2 " + String.Join("|", board[2])); | ||
| Console.WriteLine("3 " + String.Join("|", board[2])); | ||
| } | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This method is called PlaceMark, but you are checking for win or tie? seems a little out of place.