Skip to content

Welcome to my first comblete java project. Currently, you can play this game only on pc! and you need a friend to play with. It is not perfect yet ,but it is working πŸ˜πŸ‘.

Notifications You must be signed in to change notification settings

SkillSageDev/TicTacToe

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Download

If you want the source code you can download it from Releases or use git :

git clone https://github.com/SkillSageDev/TicTacToe.git
  • then run TicTacTio.exe to start playing

If you only want the game, (the .exe file), you can download it from here.

Explaining The Code

main idea

  1. ask player if they wanna play or no.

  2. if he choosed to play, the game asks him to choose a symbol, he can only choose 'x' or 'o'.

  3. print the board and asks player to choose a number.

    Alt text

    for examble: if player choosed 1, the board will change number 1 in the board to the player's symbol, then it will ask player 2 to pick a number

    Alt text

  4. the game loops until win or draw happen, then asks player if they wanna restart the game, or quit the game.

code diagram πŸ‘‡

Diagram

you can check it out from here also.

classes

  1. Board
  2. Player
  3. Menu
  4. Game

Board

main task: creat, print, and update the board.

Variables:

1. input

  • get the player input using Scanner class.

2. board

  • one dimensional array that stores numbers from (1-9) as a char.

Methods:

1. creatBoard()

  • creat a new board.

2. printBoard()

  • print the board using for loop.

Alt text

4. isValidMove(int choice)

  • make sure that there is no symbol in the given index.

3. updateBoard(int choice, char symbol)

  • the player picks a number to place his symbol, that number will be stored in choice variable.

  • symbol stores player's symbol.

  • update the board according to choice & symbol if isValidMove(choice) == true.

Player

main task: creat players with different symbols.

class variables:

1. input

  • get the player input using Scanner class.

2. symbol

  • a char variable that stores the player's symbol (x or o).

class methods:

1. chooseSymbol()

  • asks the player to choose a symbol, player can only choose (x or o).

  • return symbol.

Menu

main task: asks player to start, restart, quit the game.

class variables:

1. input

  • get the player input using Scanner class.

class methods:

1. main()

  • print main menu that asks player to start or quit game.

    Alt text

  • return input.nextInt().

2. end()

  • print end menu that asks player to restart or quit game.

    Alt text

  • return input.nextInt()

Game

main task: loop the game until win or draw.

class variables:

1. input

  • get the player input using Scanner class.

2. p1 & p2

  • objects from class Player.

3. players

  • list that stores p1 & p2.

4. menu

  • an object from class Menu.

5. board

  • an object from class Board.

6. currentPlayer

  • int variable that stores 0 or 1 to indicate player's turn.

class methods:

1. restartGame()

  • runs board.creatBoard() to print the board.

  • runs playGame() method.

2. switchPlayer()

  • it change currentPlayer to 1 or 0 by this equation currentPlayer = 1 - currentPlayer

  • so if currentPlayer = 0 when you invoke switchPlayer() it will be currentPlayer = 1 - 0 which is 1.

  • if you invoked switchPlayer() again currentPlayer = 1 - 1 which is 0.

3. replaceSymbol()

  • asks player to pick a number must be from (1-9), then start an infinit loop.

    • start a try

      • runs input.nextInt() and stores it in an int variable called choice
      • checks if board.updateBoard(choice, players[currentPlayer].symbol) == true,
      • if it is true it will break.
      • if it is false it will print invalid move and run board.updateBoard(choice, players[currentPlayer].symbol) again.
    • start a catch

      • prints invalid choice because the player didn't enter a number between (1-9).

4. winCheck()

  • it got int two diminsial array called winConditions, from the name it stores every possible win condition.

  • then runs for each loop for( : ) to store every condition from winConditions to an int array called condition.

    • checks if board.board[condition[0]] == board.board[condition[1]] && board.board[condition[1]] == board.board[condition[2]].

      • lets say in the first for loop int[] condition = {3,4,5} which means that if board.board[3] == board.board[4] == board.board[5] someone won!

      • and you know that condition[0] = 3 & condition[1] = 4 & condition[2] = 4.

      • so we do board.board[condition[0]] == board.board[condition[1]] && board.board[condition[1]] == board.board[condition[2]] to achieve the same result as board.board[3] == board.board[4] == board.board[5] .

    • return true if they are equal.

    • return false if they are not equal.

5. drawCheck()

  • runs for each loop for( : ) in board.board and store every char in the board to c variable.
  • return true if winCheck() = false & c is a symbol.
  • return false if winCheck() = false & c is a digit.

6. playTurn()

7. playGame()

  • start an infinit loop then runs PlayTurn() method.

    • check if winCheck() or drawCheck() methods equals to true.

    • runs board.printBoard() to print the board.

    • runs menu.end() to print end menu and store the return value in choice variable.

    • then runs another loop to check if choice == 1 or choice == 2 only.

      • if choice == 1 it will run restartGame() method and set choice to 2, choice = 2.

      • if choice == 2 it will break twice to get out from first loop and the second loop.

      • else runs menu.end() to print the end menu and store the input in choice variable.

        Alt text

8. setupPlayers()

  • runs players[0].chooseSymbol()
  • if player[0] choosed 'x' it will set player[1] symbol to 'o'
  • else it will set player[1] symbol to 'x'.

9. startGame()

  • runs menu.main() that asks player to start or quit game.

    Alt text

  • get his input and store it in choice variable, then start an infinit loop.

    • if choice == 1, it runs board.creatBoard() to creat a new board, and runs setupPlayers() & playGame() methods.

    • then set choice to 0.

    • if choice == 2 it runs quitGame() method then break from loop.

    • else run menu.main() again, then store the input in choice.

10. quitGame()

  • prints Thanks for playing!.

About

Welcome to my first comblete java project. Currently, you can play this game only on pc! and you need a friend to play with. It is not perfect yet ,but it is working πŸ˜πŸ‘.

Resources

Stars

Watchers

Forks

Languages