Kenavm / tictactoe

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TIC TAC TOE

In this project your job is to implement Tic-Tac-Toe for two players. You also can try writing some AI to play the game. If you find it easy, try to make it unbeatable.

What are you going to learn?

  • variables
  • functions
  • loops and conditionals
  • nested lists
  • print formatting
  • external scripts
  • user input handling
  • error handling

Tasks

Initialize the board

Implement getEmptyBoard() to return an empty 3-by-3 board, a list of lists filled with dots. The inner lists are rows.

  1. A list of lists is returned that represents a list of rows.
  2. Every cell of the returned value is ..
  3. The rows of the returned value are independent, changing one row does not affect the others.
  4. Printing the result of the getEmptyBoard() function shows the following in the terminal.
[ [ '.', '.', '.' ], [ '.', '.', '.' ], [ '.', '.', '.' ] ]

Get players' move

Implement getPlayerMove() which asks the user to make a valid move.

  1. If the user chooses an empty tile, the tile will be filled with an X or and O.
  2. The user can't mark an occupied tile.

Implement and call changePlayer() which is handle the change between the two-player.

When the user has chosen a tile, the program switch to another player.

  1. If the player starts with an X, after the move the program switch to 0 and vice versa.

Check for winners

Implement getWinningPlayer() that returns X or O based on the winning player has three of their marks in a horizontal, vertical, or diagonal row on board.

  1. The getWinningPlayer() function returns X if X has a three-in-a-row on board.
  2. The getWinningPlayer() function returns `` if there is no three-in-a-row on the board

Check for a full board

Implement isBoardFull() that returns true if the board is full.

  1. The isBoardFull() function returns true if there are no empty cells on the board.
  2. The isBoardFull() returns false if there are empty cells on the board.

Print result

The game shows if X or O or no one has won the game

  1. If player X wins, "X has won!" is displayed.
  2. If player 0 wins, "0 has won!" is displayed.
  3. If nobody wins, "It's a tie!" is displayed.

Game logic

Implement all the functions so that the game will run successfully

  1. Player X starts the game.
  2. Players alternate their moves (X, 0, X, 0...).
  3. The board is displayed before each move, and at the end of game.
  4. The game ends when someone wins or the board is full.
  5. The game handles bad input (wrong coordinates) without crashing.

Implement making a move

When the user has chosen a coordinate, that place is marked on the board.

  1. If the cell at row and col is empty (contains a dot .), it is marked with player.
  2. Out-of-bounds coordinates are not interpreted as moves.
  3. Coordinates of already occupied cells are not interpreted as moves.

Print board

Implement displayBoard() that prints the board to the screen.

  1. Players are indicated with X and 0. Empty fields are indicated with dots (.).
  2. Coordinates are displayed around the board.
  3. The board is displayed in the following format:
   1   2   3
A  . | . | .
  ---+---+---
B  . | . | .
  ---+---+---
C  . | . | .

Quit game

Allow players to quit the game anytime by typing quit in any form of capitalization (quit and QuIt both work).

  1. Typing quit instead of coordinates results in the program exiting.

AI can play

Implement player-against-AI mode. The AI can drive one of the players, and the game is fully playable against the computer.

  1. When the user selects the game mode again AI the function, getRandomAiCoordinates() or getUnbeatableAiCoordinates() is called instead of getHumanCoordinates() when it's Player 0 turn.
  2. When the user selects the game mode again AI 'AI-HUMAN', getRandomAiCoordinates() or getUnbeatableAiCoordinates() when it's Player 0 turn.
  3. Function getUnbeatableAiCoordinates() and getRandomAiCoordinates() returns a valid move (if possible) without asking for any input.
  4. Function getRandomAiCoordinates() and getUnbeatableAiCoordinates() returns "" if the board is full.
  5. Function getMenuOption() is implemented as a menu for between choosing 2-player mode and against-AI mode by pressing 1 or 2, respectively.

AI goes for easy win

AI is capable of recognizing the opportunity to win the game with one move.

  1. Function getUnbeatableAiCoordinates() picks the winning move if there is one on the board.

AI prevents easy lose

AI is capable of recognizing if its enemy could win the game with the next move, and (supposing there is no direct winning move) moves against it.

  1. Function getUnbeatableAiCoordinates() (when there is no winning move in one step) picks a move which prevents a certain winning move for its enemy.
  2. When there is a direct winning move, function getUnbeatableAiCoordinates() still picks that.
  3. When there are multiple one-step options for the enemy, getUnbeatableAiCoordinates() tries to prevent one of them.

Unbeatable AI

AI is unbeatable in all cases.

  1. There is no strategy or combination of steps that can win the game against the AI.

AI vs AI

AI can play against itself

  1. When the menu option is called with the argument 'AI-AI', it calls getUnbeatableAiCoordinates() or getRandomAiCoordinates() for both players.
  2. The game ends without any user input.
  3. There is a one second delay between moves to make gameplay easier to follow.

Hint

  • You don't have to come up with an AI strategy. You can search the internet for strategy descriptions. Do not use external code; implement written instructions instead.
  • You don't have to implement a general playing strategy. Tic-Tac-Toe has a rather easy unbeatable strategy that can be expressed as a sequence of conditionals.
  • Ideal team size is 3-4.

About


Languages

Language:JavaScript 100.0%