mirror of
https://github.com/hastagAB/Awesome-Python-Scripts.git
synced 2024-11-23 20:11:07 +00:00
TicTacToeAI and 2 players (#250)
* Add files via upload * Delete TicTacToe AI and 2 players directory * Add files via upload * Update README.md
This commit is contained in:
parent
1d2de84900
commit
42764d2647
|
@ -11,6 +11,7 @@ So far, the following projects have been integrated to this repo:
|
||||||
|
|
||||||
| Project Name | Contributors |
|
| 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) |
|
| [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) |
|
| [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)|
|
|[File Encrypt Decrypt](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/file-encrypt-decrypt)|[Aditya Arakeri](https://github.com/adityaarakeri)|
|
||||||
|
|
3
TicTacToe_AI_and_2_players/README.md
Normal file
3
TicTacToe_AI_and_2_players/README.md
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
author: Omar Sameh
|
||||||
|
email: o.s.dr.who@gmail.com
|
||||||
|
no requirments enjoy!
|
1
TicTacToe_AI_and_2_players/Requirements.txt
Normal file
1
TicTacToe_AI_and_2_players/Requirements.txt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
None! enjoy!
|
53
TicTacToe_AI_and_2_players/TicTacToe2players.py
Normal file
53
TicTacToe_AI_and_2_players/TicTacToe2players.py
Normal file
|
@ -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("")
|
60
TicTacToe_AI_and_2_players/TicTacToeAI.py
Normal file
60
TicTacToe_AI_and_2_players/TicTacToeAI.py
Normal file
|
@ -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
|
67
TicTacToe_AI_and_2_players/TicTacToeVsAI.py
Normal file
67
TicTacToe_AI_and_2_players/TicTacToeVsAI.py
Normal file
|
@ -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("")
|
Loading…
Reference in New Issue
Block a user