Skip to content

Commit e289d35

Browse files
committed
Adding game files
1 parent 73d0c76 commit e289d35

File tree

6 files changed

+344
-0
lines changed

6 files changed

+344
-0
lines changed

GAMES/Tic-Tac-Bot-Player/bot.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from packages import module_for_bot
2+
from packages import common_module
3+
from packages import config
4+
5+
6+
def script_with_bot():
7+
"""Main block of code which runs when bot-mode gameplay is selected"""
8+
9+
# Default matrix
10+
config.the_matrix = [["_", "_", "_"], ["_", "_", "_"], ["_", "_", "_"]]
11+
12+
# Default empty_cells
13+
config.empty_cells = 9
14+
15+
# Printing the game box
16+
common_module.printer(config.the_matrix)
17+
18+
while config.empty_cells:
19+
20+
# To check if the user has entered valid co-ordinates of matrix
21+
is_success = common_module.board_modifier(config.the_matrix)
22+
while is_success == -1:
23+
print("Try another co-ordinates: ")
24+
is_success = common_module.board_modifier(config.the_matrix)
25+
26+
common_module.printer(config.the_matrix)
27+
28+
# 'state' will be 'None' if the game is not finished yet
29+
state = module_for_bot.bot_turn(config.the_matrix)
30+
31+
# Exiting the 'while' loop if a valid end-game state has been achieved
32+
if state is not None:
33+
break
34+
35+
if state == 1:
36+
print('The bot has won the game...\n')
37+
elif state == -1:
38+
print('The player has won the game!\n')
39+
else:
40+
print('The game is a draw!\n')

GAMES/Tic-Tac-Bot-Player/main.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import with_person
2+
import bot
3+
4+
5+
def choice_checker(choice):
6+
"""This checks if the user has entered a valid choice"""
7+
8+
global choices
9+
while choice not in choices:
10+
print("Enter a valid choice!")
11+
print("""Choose the mode of game.
12+
'1' to play with a friend,
13+
'2' to play with a bot (P.S: You might not win)
14+
'3' to quit this\n""")
15+
choice = input()
16+
return choice
17+
18+
19+
# Giving the user a choice to select the mode of the game they want to play
20+
choices = ['1', '2', '3']
21+
print("""Choose the mode of game.
22+
'1' to play with a friend,
23+
'2' to play with a bot (P.S: You might not win)
24+
'3' to quit this\n""")
25+
choice = input()
26+
choice = choice_checker(choice)
27+
28+
while True:
29+
if choice == choices[0]:
30+
with_person.script_with_person()
31+
elif choice == choices[1]:
32+
bot.script_with_bot()
33+
else:
34+
break
35+
36+
print("Would you like to try again..?")
37+
print("""Choose the mode of game.
38+
'1' to play with a friend,
39+
'2' to play with a bot (P.S: You might not win)
40+
'3' to quit this\n""")
41+
choice = input()
42+
choice = choice_checker(choice)
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
from os import system
2+
from time import sleep
3+
from packages import config
4+
5+
6+
def printer(the_matrix):
7+
"""Prints the game board"""
8+
9+
print()
10+
print('-' * 9)
11+
for i in range(3):
12+
print('|', end=' ')
13+
for j in range(3):
14+
if the_matrix[i][j] == '_' or the_matrix[i][j] == ' ':
15+
print("_ ", end='')
16+
else:
17+
print(the_matrix[i][j], end=' ')
18+
print('|')
19+
print('-' * 9)
20+
print()
21+
22+
23+
def check_for_draw():
24+
if config.empty_cells != 0:
25+
return False
26+
else:
27+
return True
28+
29+
30+
def the_judge():
31+
"""This function calls another function and sees if any one player has won"""
32+
# Checking the game state
33+
x_found = finder('X')
34+
o_found = finder('O')
35+
36+
# If either one of them is found in a row, it wins the game
37+
if x_found:
38+
return 'X'
39+
elif o_found:
40+
return 'O'
41+
else:
42+
return None
43+
44+
45+
def finder(letter):
46+
"""Returns True if the win condition is achieved for a specific letter(player) in the board, else returns False"""
47+
48+
# Checking by ROWS
49+
matrix = config.the_matrix
50+
for i in range(0, 3):
51+
counter = 0
52+
for j in range(0, 3):
53+
if matrix[i][j] == letter:
54+
counter += 1
55+
56+
# Checking if the letter is found in a row
57+
if counter == 3:
58+
return True
59+
60+
# Checking by COLUMNS
61+
for i in range(0, 3):
62+
counter = 0
63+
for j in range(0, 3):
64+
if matrix[j][i] == letter:
65+
counter += 1
66+
67+
if counter == 3:
68+
return True
69+
70+
# Checking by first diagonal by indices
71+
counter = 0
72+
for i in range(0, 3):
73+
if matrix[i][i] == letter:
74+
counter += 1
75+
76+
if counter == 3:
77+
return True
78+
79+
# Checking by second diagonal by indices
80+
counter = 0
81+
j_index = 2
82+
for i in range(0, 3):
83+
if matrix[i][j_index] == letter:
84+
counter += 1
85+
j_index -= 1
86+
87+
if counter == 3:
88+
return True
89+
90+
return False
91+
92+
93+
def board_modifier(the_matrix, player=None):
94+
"""Modifies the game board as per the player's moves.
95+
Behaves dynamically to the mode of the game(with person, or with a bot)"""
96+
97+
sleep(0.5)
98+
the_coords = input("Player's turn: ").split() if player is None else input().split()
99+
if len(the_coords) != 2:
100+
print('Enter two coordinate numbers!')
101+
return -1
102+
x, y = the_coords
103+
try:
104+
for index in the_coords:
105+
index = int(index)
106+
except ValueError:
107+
print('You should enter numbers!')
108+
return -1
109+
else:
110+
x = int(x)
111+
y = int(y)
112+
if x not in range(1, 4) or y not in range(1, 4):
113+
print('Coordinates should be from 1 to 3!')
114+
return -1
115+
else:
116+
x, y = x-1, y-1
117+
if the_matrix[x][y] != '_':
118+
print('This cell is occupied! Choose another one!')
119+
return -1
120+
else:
121+
if player is None:
122+
from packages import module_for_bot
123+
module_for_bot.modify_board_bot('X', [x, y], the_matrix=the_matrix)
124+
else:
125+
the_matrix[x][y] = player
126+
# config.turns += 1
127+
# Clearing the terminal and writing the updated matrix
128+
system('cls')
129+
if player is None:
130+
pass
131+
else:
132+
printer(the_matrix)
133+
config.empty_cells -= 1
134+
return 2
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
the_matrix = [["_", "_", "_"], ["_", "_", "_"], ["_", "_", "_"]]
2+
empty_cells = 9
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
from packages import common_module, config
2+
from time import sleep
3+
4+
5+
def modify_board_bot(letter, position, the_matrix):
6+
"""Simultaneously updates the matrix and checks for a win or draw condition.
7+
This function is only specific to the bot-mode of the game
8+
"""
9+
10+
the_matrix[position[0]][position[1]] = letter
11+
12+
check_for_win = common_module.the_judge()
13+
if check_for_win == 'X':
14+
print('The player has won the game!\n')
15+
return -1
16+
elif check_for_win == 'O':
17+
# print('The bot has won the game...\n')
18+
return 1
19+
elif common_module.check_for_draw():
20+
print('The game is a draw!\n')
21+
return 0
22+
else:
23+
return None
24+
25+
26+
def the_algorithm(the_matrix, is_bot):
27+
"""This function is the brain of the bot"""
28+
29+
if common_module.finder('O'):
30+
return 1
31+
elif common_module.finder('X'):
32+
return -1
33+
elif common_module.check_for_draw():
34+
return 0
35+
36+
if is_bot:
37+
best_score = -1000
38+
for row in range(3):
39+
for column in range(3):
40+
if the_matrix[row][column] == '_':
41+
the_matrix[row][column] = 'O'
42+
score = the_algorithm(the_matrix, False)
43+
the_matrix[row][column] = '_'
44+
45+
if score > best_score:
46+
best_score = score
47+
return best_score
48+
else:
49+
best_score = 1000
50+
for row in range(3):
51+
for column in range(3):
52+
if the_matrix[row][column] == '_':
53+
the_matrix[row][column] = 'X'
54+
score = the_algorithm(the_matrix, True)
55+
the_matrix[row][column] = '_'
56+
if score < best_score:
57+
best_score = score
58+
return best_score
59+
60+
61+
def bot_turn(the_matrix):
62+
"""Decides which move the bot has to do."""
63+
64+
print('The bot\'s turn: ')
65+
sleep(0.3)
66+
print('The bot is thinking..')
67+
sleep(0.8)
68+
best_score = -1000
69+
best_move = [-1, -1]
70+
71+
for row in range(3):
72+
for column in range(3):
73+
if the_matrix[row][column] == '_':
74+
the_matrix[row][column] = 'O'
75+
current_score = the_algorithm(the_matrix, False)
76+
the_matrix[row][column] = '_'
77+
if current_score > best_score:
78+
best_score = current_score
79+
best_move = row, column
80+
config.empty_cells -= 1
81+
value = modify_board_bot('O', best_move, the_matrix=the_matrix)
82+
common_module.printer(the_matrix)
83+
return value
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from packages import config
2+
from packages import common_module
3+
4+
5+
def script_with_person():
6+
"""Main block of code which runs when bot-mode gameplay is selected"""
7+
8+
# Default matrix
9+
config.the_matrix = [["_", "_", "_"], ["_", "_", "_"], ["_", "_", "_"]]
10+
11+
# Default empty_cells
12+
config.empty_cells = 9
13+
14+
# Printing the game box
15+
common_module.printer(config.the_matrix)
16+
17+
# Setting the players for the game to proceed by taking turns
18+
player_1 = 'X'
19+
player_2 = 'O'
20+
current_player = player_1
21+
possibilities = ['X', 'O', 'None']
22+
while config.empty_cells:
23+
print("Player 1's turn: ") if current_player == 'X' else print("Player 2's turn: ")
24+
25+
# Making sure player enters correct co-ordinates
26+
is_success = common_module.board_modifier(config.the_matrix, current_player)
27+
while is_success == -1:
28+
print("Try another co-ordinates: ")
29+
is_success = common_module.board_modifier(config.the_matrix, current_player)
30+
31+
# Switching the player turn
32+
current_player = player_1 if current_player == player_2 else player_2
33+
34+
# Checking if there is any win condition for the current board state after the player's move
35+
judgement = common_module.the_judge()
36+
37+
if judgement in possibilities and config.empty_cells == 0:
38+
break
39+
40+
if judgement == 'None':
41+
print(f'The game is a {judgement}\n')
42+
else:
43+
print('Player 1 wins!\n') if judgement == 'X' else print('Player 2 wins!\n')

0 commit comments

Comments
 (0)