Git Product home page Git Product logo

tic-tac-toe's Introduction

GitHub GitHub last commit GitHub issues

DeepSource

Tic-Tac-Toe with AI


Description

A simple implementation of tic-tac-toe: You can play with the AI or with a friend. If you get bored you can watch two AI's fighting too.

To participate, select the user mode. As a player, you can select which position to mark by entering the column (numbers at bottom) and row (numbers on the left side). See the Example section.

For AI, there is easy, medium, and unbeatable mode.

The easy mode simply makes random moves.

The medium mode AI makes a move using the following process:

  1. If it can win in one move (if it has two in a row), it places a third to get three in a row and win.
  2. If the opponent can win in one move, it plays the third itself to block the opponent to win.
  3. Otherwise, it makes a random move.

The unbeatable mode AI uses minmax algorithm which makes it unbeatable.

Example

The example below shows how the program works. The greater-than symbol followed by space (>) represents the user input. Notice that it's not the part of the input.

Enter mode for Player 1: > user
Enter mode for Player 2: > medium
  ---------
3 |       |
2 |       |
1 |       |
  ---------
    1 2 3
Enter the coordinates: > 2 2
  ---------
3 |       |
2 |   X   |
1 |       |
  ---------
    1 2 3
Making move level "medium"
  ---------
3 |       |
2 |   X   |
1 | O     |
  ---------
    1 2 3
Enter the coordinates: > 1 3
  ---------
3 | X     |
2 |   X   |
1 | O     |
  ---------
    1 2 3
Making move level "medium"
  ---------
3 | X     |
2 |   X   |
1 | O   O |
  ---------
    1 2 3
Enter the coordinates: > 2 1
  ---------
3 | X     |
2 |   X   |
1 | O X O |
  ---------
    1 2 3
Making move level "medium"
  ---------
3 | X O   |
2 |   X   |
1 | O X O |
  ---------
    1 2 3
Enter the coordinates: > 1 2
  ---------
3 | X O   |
2 | X X   |
1 | O X O |
  ---------
    1 2 3
Making move level "medium"
  ---------
3 | X O   |
2 | X X O |
1 | O X O |
  ---------
    1 2 3
Enter the coordinates: > 3 3
  ---------
3 | X O X |
2 | X X O |
1 | O X O |
  ---------
    1 2 3
Draw

Enter mode for Player 1: > medium
Enter mode for Player 2: > user
  ---------
3 |       |
2 |       |
1 |       |
  ---------
    1 2 3
Making move level "medium"
  ---------
3 |       |
2 |       |
1 |   X   |
  ---------
    1 2 3
Enter the coordinates: > 2 2
  ---------
3 |       |
2 |   O   |
1 |   X   |
  ---------
    1 2 3
Making move level "medium"
  ---------
3 |       |
2 |   O   |
1 | X X   |
  ---------
    1 2 3
Enter the coordinates: > 3 1
  ---------
3 |       |
2 |   O   |
1 | X X O |
  ---------
    1 2 3
Making move level "medium"
  ---------
3 | X     |
2 |   O   |
1 | X X O |
  ---------
    1 2 3
Enter the coordinates: > 1 2
  ---------
3 | X     |
2 | O O   |
1 | X X O |
  ---------
    1 2 3
Making move level "medium"
  ---------
3 | X     |
2 | O O X |
1 | X X O |
  ---------
    1 2 3
Enter the coordinates: > 3 3
  ---------
3 | X   O |
2 | O O X |
1 | X X O |
  ---------
    1 2 3
Making move level "medium"
  ---------
3 | X X O |
2 | O O X |
1 | X X O |
  ---------
    1 2 3
Draw

Enter mode for Player 1: > exit

License

This project is licensed under the MIT License. For details, check the LICENSE file.

tic-tac-toe's People

Contributors

deepsource-autofix[bot] avatar deepsourcebot avatar gokulrajanpillai avatar j-tesla avatar koleo avatar restyled-commits avatar restyled-io[bot] avatar

Watchers

 avatar  avatar

tic-tac-toe's Issues

New Features to add

Hi there, I would like to contribute to the project and I would like to express my interest and make several changes to the original, there are things that I would like to change, firstly is that there should be the ability to allow the user to exit in the middle of the game, then, the program should allow the user to add in their name, after that, there should be option for the user to be able to play against another user, following that, the program should have the option to have more than 3x3 grid size e.g. the can choose the grid size of 3x3, 4x4, 5x5, and in addition the game should allow to play for more than 1 rounds, either 1 round or 3 rounds.

Add unbeatable mode

Minmax algorithm works perfectly for games with a small number of turns like tic-tac-toe. The unbeatable mode or the hard mode can be implemented by it.

make better guide about input and coordinates of the board

Hello,

I wanted to tell you that the inputing a coordinate is confusing. So give a
better guide there

On Sun, 27 Sep 2020, 12:44 Jayanth PSY, [email protected] wrote:

@kusalhasintha https://github.com/kusalhasintha can you kindly what
this PR is about?
Did you try to open an issue?
Those changes in README.md seem unnecessary.

โ€”
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#5 (comment),
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AHMQGCZBWHYA6YZEQGBNDEDSH3Q3TANCNFSM4R3MHYOA
.

Originally posted by @kusalhasintha in #5 (comment)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.