-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
133 lines (113 loc) · 3.23 KB
/
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
import random
import visual
from board import *
from player import Player
class TicTacToe:
"""A class representing the game"""
def switch_turn(cur_player, player1, player2):
"""Switch the users turns"""
if cur_player == player1:
return player2
else:
return player1
def ask_place():
"""Ask user to put in row and column, returns as a tuple."""
print("Which row would you like to put it in?")
try:
row = int(input())
print("Which column would you like to put it in?")
column = int(input())
assert (0 < row < 4 and 0 < column < 4)
return row-1, column-1
except:
print("Your row or column is not a valid place on the board")
return ask_place()
def reset_game(board) :
"""Shall clear the terminal and prepare for next game"""
# clear the board
board.reset_board()
board.display()
#
def main():
"""Loops through game and prints and has the structure for a simple
game of tic tac toe."""
# init game components
board = Board(3, 3)
player_one = Player("X")
player_two = Player("O")
board.display()
# check
continue_playing = True
current_player = random.choice([player_one, player_two])
while (continue_playing):
print(str(current_player.playing_piece) + " turn")
# gets a valid place to put piece
row, col = ask_place()
while (board.is_taken(row, col)):
print("Is already taken, try another place!")
row, col = ask_place()
current_player.place_piece(row, col, board)
board.display()
# check if game is over
if (board.has_won(current_player)):
print(str(current_player.playing_piece) + " has won")
play_again = None
print("Would you like to play again? y/n!")
while (play_again != "y" and play_again != "n") :
play_again = input()
if (play_again == "y") :
reset_game(board)
else :
continue_playing = False
# in case of draw
elif (board.is_full()) :
print("It is a tie, would you like to play again? y/n!")
play_again = None
while (play_again != "y" and play_again != "n") :
play_again = input()
if (play_again == "y") :
reset_game(board)
pass
else :
continue_playing = False
current_player = switch_turn(current_player, player_one, player_two)
def four_in_a_row():
"""Play a game of 4 in a row"""
# init visuals
visual.display_surface
player1 = Player("X")
player2 = Player("O")
board = FourInARowBoard(6, 7)
board.display()
playing = True
current_player = random.choice([player1, player2])
while (playing):
print(str(current_player.playing_piece) + " turn.")
column = 0
while(True):
try:
column = int(input())
except:
pass
if (0 < column <= board.COLUMNS):
if (board.is_valid(column-1)):
break
else:
print("Can not put piece there! Please insert another column.")
else:
print("Input was not valid, needs to be in bounds! Try again.")
row, col = board.place_piece(column-1, current_player)
if(current_player.playing_piece == "X"):
visual.draw_x(row, col)
else:
visual.draw_circle(row, col)
board.display()
# check if won
# if won congratulate and break
if (board.has_won(current_player, x, y)):
print(f"Congratulations! {current_player} has won the game!!!")
break
# else continue
current_player = switch_turn(current_player, player1, player2)
four_in_a_row()
# main()