From 42764d264730c700aa669ab7988406cbf3eaf866 Mon Sep 17 00:00:00 2001 From: Omar Sameh <72161983+ShadowHunter15@users.noreply.github.com> Date: Tue, 8 Feb 2022 09:27:29 +0300 Subject: [PATCH] TicTacToeAI and 2 players (#250) * Add files via upload * Delete TicTacToe AI and 2 players directory * Add files via upload * Update README.md --- README.md | 1 + TicTacToe_AI_and_2_players/README.md | 3 + TicTacToe_AI_and_2_players/Requirements.txt | 1 + .../TicTacToe2players.py | 53 +++++++++++++++ TicTacToe_AI_and_2_players/TicTacToeAI.py | 60 +++++++++++++++++ TicTacToe_AI_and_2_players/TicTacToeVsAI.py | 67 +++++++++++++++++++ 6 files changed, 185 insertions(+) create mode 100644 TicTacToe_AI_and_2_players/README.md create mode 100644 TicTacToe_AI_and_2_players/Requirements.txt create mode 100644 TicTacToe_AI_and_2_players/TicTacToe2players.py create mode 100644 TicTacToe_AI_and_2_players/TicTacToeAI.py create mode 100644 TicTacToe_AI_and_2_players/TicTacToeVsAI.py diff --git a/README.md b/README.md index 39e8ed8..c3a3716 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ So far, the following projects have been integrated to this repo: | Project Name | Contributors | |--|--| +| [TicTacToe AI and 2 players](https://github.com/ShadowHunter15/Awesome-Python-Scripts/tree/master/TicTacToe_AI_and_2_players) | [Omar Sameh](https://github.com/ShadowHunter15) | | [AI for guess the number](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/AI_for_Guess_the_number) | [Omar Sameh](https://github.com/ShadowHunter15) | | [sudoku-solver](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/sudoku-solver) | [Rishabh Umrao](https://github.com/ayedaemon) | |[File Encrypt Decrypt](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/file-encrypt-decrypt)|[Aditya Arakeri](https://github.com/adityaarakeri)| diff --git a/TicTacToe_AI_and_2_players/README.md b/TicTacToe_AI_and_2_players/README.md new file mode 100644 index 0000000..afe2e1d --- /dev/null +++ b/TicTacToe_AI_and_2_players/README.md @@ -0,0 +1,3 @@ +author: Omar Sameh +email: o.s.dr.who@gmail.com +no requirments enjoy! \ No newline at end of file diff --git a/TicTacToe_AI_and_2_players/Requirements.txt b/TicTacToe_AI_and_2_players/Requirements.txt new file mode 100644 index 0000000..f8a85aa --- /dev/null +++ b/TicTacToe_AI_and_2_players/Requirements.txt @@ -0,0 +1 @@ +None! enjoy! \ No newline at end of file diff --git a/TicTacToe_AI_and_2_players/TicTacToe2players.py b/TicTacToe_AI_and_2_players/TicTacToe2players.py new file mode 100644 index 0000000..4aac97c --- /dev/null +++ b/TicTacToe_AI_and_2_players/TicTacToe2players.py @@ -0,0 +1,53 @@ +import random +board = [" "," "," "," "," "," "," "," "," "] +def display_board(): + print("") + print("| "+board[0]+" | "+board[1]+" | "+board[2]+" | ") + print("| "+board[3]+" | "+board[4]+" | "+board[5]+" | ") + print("| "+board[6]+" | "+board[7]+" | "+board[8]+" | ") + print("") +def wincheck(mark): + return((board[0]==mark and board[1]== mark and board[2]==mark )or #for row1 + (board[3]==mark and board[4]==mark and board[5]==mark )or + (board[6]==mark and board[7]==mark and board[8]==mark )or + (board[0]==mark and board[3]==mark and board[6]== mark )or + (board[1]==mark and board[4]==mark and board[7]==mark )or + (board[2]==mark and board[5]==mark and board[8]==mark )or + (board[0]==mark and board[4]==mark and board[8]==mark )or + (board[2]==mark and board[4]==mark and board[6]==mark )) +def make_turn(turn, pos): + if turn: + letter = "O" + else: + letter = "X" + board[pos-1] = letter + display_board() +turn = random.randint(0, 1) +#display_board() +print(""" +board alignment: + +| 1 | 2 | 3 | +| 4 | 5 | 6 | +| 7 | 8 | 9 | + +""") +while True: + if turn: + player = "O" + else: + player = "X" + pos= int(input(player + "'s turn: ")) + if board[pos-1] != " ": + print("Taken, choose another") + continue + make_turn(turn, pos) + if wincheck(player): + print(player + " wins!") + break + elif " " not in board: + print("Draw") + break + turn = not turn + print("-" * 20) +input("") diff --git a/TicTacToe_AI_and_2_players/TicTacToeAI.py b/TicTacToe_AI_and_2_players/TicTacToeAI.py new file mode 100644 index 0000000..ed0cb66 --- /dev/null +++ b/TicTacToe_AI_and_2_players/TicTacToeAI.py @@ -0,0 +1,60 @@ +import random +def wincheck(board): + score = { + "O": 1, + "X": -1 + } + for mark in ["O", "X"]: + if ((board[0]==mark and board[1]== mark and board[2]==mark )or + (board[3]==mark and board[4]==mark and board[5]==mark )or + (board[6]==mark and board[7]==mark and board[8]==mark )or + (board[0]==mark and board[3]==mark and board[6]== mark )or + (board[1]==mark and board[4]==mark and board[7]==mark )or + (board[2]==mark and board[5]==mark and board[8]==mark )or + (board[0]==mark and board[4]==mark and board[8]==mark )or + (board[2]==mark and board[4]==mark and board[6]==mark )): + return score[mark] + if " " not in board: + return 0 +depth = [0] +def bestMove(board): + if depth[0] == 1: + return random.choice([i+1 for i in range(len(board)) if board[i] == " "]) + best = float("inf") + move = 0 + if board.count(" ") in [8, 9] and board[4] == " ": + return 5 + for i in range(len(board)): + if board[i] == " ": + board[i] = "X" + Try = findBestMove(board, 1, depth[0]) + if Try < best: + best = Try + move = i + board[i] = " " + return move+1 +def findBestMove(board, maximizing, depth): + if wincheck(board) is not None: + return wincheck(board) + if depth > 0: + depth -= 1 + if maximizing == 1: + best = float("-inf") + for i in range(len(board)): + if board[i] == " ": + board[i] = "O" + Try = findBestMove(board, 0, depth) + board[i] = " " + best = max(best, Try) + return best + if maximizing == 0: + best = float("inf") + for i in range(len(board)): + if board[i] == " ": + board[i] = "X" + Try = findBestMove(board, 1, depth) + board[i] = " " + best = min(best, Try) + return best + else: + return 0 diff --git a/TicTacToe_AI_and_2_players/TicTacToeVsAI.py b/TicTacToe_AI_and_2_players/TicTacToeVsAI.py new file mode 100644 index 0000000..3fa451e --- /dev/null +++ b/TicTacToe_AI_and_2_players/TicTacToeVsAI.py @@ -0,0 +1,67 @@ +import random +import TicTacToeAI +depth = int(input( +"""Choose difficulty: + + Easy: 1 + Medium: 2 + Hard: 3 + +choice: """)) +print(""" +board alignment: + +| 1 | 2 | 3 | +| 4 | 5 | 6 | +| 7 | 8 | 9 | + +""") +TicTacToeAI.depth[0] = depth +board = [" "," "," "," "," "," "," "," "," "] +def display_board(): + print("") + print("| "+board[0]+" | "+board[1]+" | "+board[2]+" | ") + print("| "+board[3]+" | "+board[4]+" | "+board[5]+" | ") + print("| "+board[6]+" | "+board[7]+" | "+board[8]+" | ") + print("") +def wincheck(mark): + return((board[0]==mark and board[1]== mark and board[2]==mark )or #for row1 + (board[3]==mark and board[4]==mark and board[5]==mark )or + (board[6]==mark and board[7]==mark and board[8]==mark )or + (board[0]==mark and board[3]==mark and board[6]== mark )or + (board[1]==mark and board[4]==mark and board[7]==mark )or + (board[2]==mark and board[5]==mark and board[8]==mark )or + (board[0]==mark and board[4]==mark and board[8]==mark )or + (board[2]==mark and board[4]==mark and board[6]==mark )) +def make_turn(turn, pos): + if turn: + letter = "O" + else: + letter = "X" + board[pos-1] = letter + display_board() +turn = random.randint(0, 1) +#display_board() +while True: + if turn: + player = "O" + else: + player = "X" + if player == "O": + pos= int(input(player + "'s turn: ")) + else: + pos = TicTacToeAI.bestMove(board) + if board[pos-1] != " ": + print("Taken, choose another") + continue + make_turn(turn, pos) + if wincheck(player): + print(player + " wins!") + break + elif " " not in board: + print("Draw") + break + turn = not turn + print("-" * 20) + +input("")