Giter Site home page Giter Site logo

connect_4_ai's Introduction

connect_4_AI

An algorithm to win the famous "connect 4" game.

Using the minimax algorithm, choose the best move to play.
The algorithm searches for the best move it can make by considering the possible outcomes in the future of the game. The algorithm looks into the future in a certain depth, and in that scope of view it plays the game perfectly.
If the algorithm finds a way that theoreticly it would lose (if the opponent will play perfectly), it will delay the lost or avoid it as good as posible.

Usage/Examples

  • To play against the AI with simple ASCII preview:

    from AI import play_against_ai
    from Board import Board
    
    board = Board(size=(7, 6))
    play_against_ai(board, depth=5, you_start=True)
  • To play against the AI or a friend with GUI preview:

    from play_with_gui import play
    from Board import Board
    
    board = Board(size=(7, 6))
    play(board, ai=True, ai_starts=False, ai_depth=5, cell_width=100, wait_between_turns=0)

Notice: Do not increase the AI depth parameter or the size of the board too much. It will take long time for the algorithm to finish running.

Requirements

  • python3 and above.
  • for the GUI version- pygame. To install use:
    pip install pygame
    in your terminal.

Screenshots

connect_4_ai's People

Contributors

yotamzaiger1 avatar

Watchers

 avatar

connect_4_ai's Issues

AI Don't Win As Fast As It Can

If the best_move algorithm findes a way to win the game, it will take that pass even if there is a faster way to win.
Try this code:

for x in [3,3,2,4,4,3,1,0,5,0,0,1,2,2,4,1,1]:
....b.turn(x)
print(b)
b.printb()
print(best_move(b, 7, maximizing_player=False))

It returns 2, even though it should had taken column number 3.

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.