-
Notifications
You must be signed in to change notification settings - Fork 0
/
othello_main.py
165 lines (143 loc) · 5.91 KB
/
othello_main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# one player mode is still in the making
# this file contains the major chunk of the code
import os
from time import sleep
import sys
from itertools import cycle
from othello_board import flippy
from othello_possibilities import board_possi
def cleanup():
os.system('cls')
print("O T H E L L O\n\n")
def board_refresh():
global board, symdict, numdict, letdict
board = [[0 for i in range(8)] for i in range(8)]
board[4][4], board[3][3] = 1, 1
board[3][4], board[4][3] = 2, 2
def showboard():
global board, symdict, letdict, numdict
dashes = "\t" + "-"*49
print("\t ", end="")
for i in range(8):
print(numdict[i+1], end=" ")
for i in range(8):
print("\n"+dashes)
print(" "*5+f"{i+1} ", end="")
for j in range(8):
print(f"| {symdict[board[i][j]]} ", end="")
print("|", end="")
print("\n"+dashes)
def letsgo():
global board, symdict, letdict, numdict
board_refresh()
exit = False
final_exit = False
turns = 0
for i in cycle((1, 2)): # keep scycling between player 1 and 2 till break statement is triggered when moves are over
cleanup()
print(
f"Here's the board, type a letter followed by an integer to denote the position:\nType 'exit' at any point to leave mid-game.\n")
board = board_possi(board, i)
white_num = 0
black_num = 0
move_poss = False
for j in board: # count numbers for each type of piece
for k in j:
if k == 1:
white_num += 1
if k == 2:
black_num += 1
if k == 3:
move_poss = True # check if any moves are left while iterating over the loop
showboard()
print(
f"\n\t{symdict[1]} = {white_num} {symdict[2]} = {black_num}\n")
col_play = 0
row_play = 0
while move_poss: # keep doing this until a valid input is given and moves are left
print(f"{symdict[i]} - Now make your move, player-{i}:")
play = str(input())
if play == "exit": # check if player wants to quit first, then see if their input is valid
exit = True
break
try:
if play[0].lower() in "abcdefgh":
col_play = letdict[play[0].upper()] - 1
if play[1] in "12345678":
row_play = int(play[1])-1
if board[row_play][col_play] == 3:
board[row_play][col_play] = i
board = flippy(board, row_play, col_play)
turns += 1
break
else:
print(
f"\nYou can only place a piece on the empty spaces that lead to flips. All such options are marked with '{symdict[3]}'")
else:
print(
"\nplease type a letter followed by an integer for a position on the board.")
else:
print(
"\nPlease type a letter followed by an integer for a position on the board.")
except IndexError:
print("\nYou need to type both coordinates.")
else:
cleanup()
exit = True
print(f"Player-{i} has no valid moves left...")
if exit or turns == 60:
for i in range(8): # makes sure no more possibility indicators on the board if quit
for j in range(8):
if board[i][j] == 3:
board[i][j] = 0
print("Here is the final board and score:\n")
showboard()
print(
f"\n\t{symdict[1]} = {white_num} {symdict[2]} = {black_num}\n")
if white_num > black_num: # print which player has more pieces
print(f"\n{symdict[1]} player-1 WINS!\n")
elif black_num > white_num:
print(f"\n{symdict[2]} player-2 WINS!\n")
else:
print("\nIt's a perfect tie...? That's really rare!\n")
while True: # ask to play again
print("\nWould you like to play again?\n(A) Yes (B) No")
try:
again = str(input())
again.lower()
except:
print("Please give a valid input. There are only two options")
continue
if again == "a" or again == "yes":
letsgo()
break
elif again == "b" or again == "no":
final_exit = True
break
else:
print("Please give a valid input. There are only two options")
continue
if final_exit == True:
print("\nIt was nice to have you! Byeee!\n")
quit()
cleanup()
while True:
print("How many players?\n(A) 1\t(B) 2\n")
players = input()
if str(players) in "bB2":
cleanup()
print("Let's get started then!\nEach player just has to type the number corresponding to the position where they wish to make their mark.", end="")
sys.stdout.flush()
for i in range(4):
print(".", end="")
sleep(1)
sys.stdout.flush()
symdict = {0: " ", 1: "[_]", 2: "###", 3: " ~ "}
letdict = {"A": 1, "B": 2, "C": 3, "D": 4,
"E": 5, "F": 6, "G": 7, "H": 8}
numdict = {v: k for k, v in letdict.items()}
letsgo()
break
else:
cleanup()
print("We only have 2 a player game right now, sorry for the inconvenience.")