-
Notifications
You must be signed in to change notification settings - Fork 1
/
xo.py
executable file
·154 lines (136 loc) · 3.75 KB
/
xo.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
#! /usr/bin/python3
from enum import Enum
from board import board, err
import math
from numpy import *
VERBOSE = 0
def transpose(board):
transBoard = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
c = 0
for b in board:
r = 2
for e in b:
transBoard[r][c] = e
r -= 1
c += 1
return transBoard
"""
A generalized tic tac toe game class.
"""
class xo:
def __init__(self, M, N, K, sym, order):
"""
Initilize a game with a board of size M x N with K players using the symbols in the list sym.
The players play in the order specified by the list order.
"""
self.brd = board(M, N, K, sym)
self.play_order = order
self.num_players = K
self.reset()
def reset(self):
self.brd.reset()
self.game_turn = 0
self.gameLog = ones(((self.brd.row * self.brd.col), 2), int)*-1
self.game_over = False
self.turn = self.play_order[0]
def mark(self, pos, player):
"""
Marks a position on the board with the symbol for the player.
If the position is already marked, the function returns err.INVALID_MOVE
If a player attempts to play out of turn, the function returns err.OUT_OF_TURN
"""
# Are we trying to play after the game is over?
if(not self.game_over):
# Is a player trying to play out of turn?
if(player == self.play_order[(self.game_turn % self.num_players)]):
returnVal = self.brd.set_pos(pos, player)
# is the postion selected to mark is not invalid?
if(returnVal == err.OK):
self.gameLog[self.game_turn] = [player, pos]
self.game_turn += 1
# Do we have a winner?
returnVal = self.has_won(player)
if (returnVal == err.WIN):
# Yes. We do have a winner
self.game_over = True
else:
# Cheat! Did you think you could get away by playing out of turn? Well, you cant!
console_log(1, "OUT OF TURN")
returnVal = err.OUT_OF_TURN
else:
# yes. A player tried to play after the game was over... Droids !!!
returnVal = err.INVALID_MOVE
return returnVal
def get_board(self):
return self.brd.get_board_str()
def has_won(self, player):
"""
Implemented for standard 3x3 tic tac toe game
"""
if (self.brd.free_positions == 0):
self.game_over = True
win_logic = (self.brd.board == player)
# Check linear
for i in range(0, 2):
lin_sum = sum(sum(win_logic, i) == 3)
if (lin_sum == 1):
returnVal = err.WIN
break
else:
returnVal = err.OK
# check diagonals
if (returnVal == err.OK):
if (((sum(diagonal(win_logic))) == 3) |
((sum(diagonal(transpose(win_logic)))) == 3)):
returnVal = err.WIN
if ((self.game_over) & (returnVal == err.OK)):
returnVal = err.DRAW
return returnVal
def stateless_has_won(self, board, player):
"""
Implemented for standard 3x3 tic tac toe game
"""
game_over = False
free_positions = sum(sum(board == 0))
if (free_positions == 0):
game_over = True
win_logic = (board == player)
# Check linear
for i in range(0, 2):
lin_sum = sum(sum(win_logic, i) == 3)
if (lin_sum == 1):
returnVal = err.WIN
break
else:
returnVal = err.OK
# check diagonals
if (returnVal == err.OK):
if (((sum(diagonal(win_logic))) == 3) | ((sum(diagonal(transpose(win_logic)))) == 3)):
returnVal = err.WIN
if ((game_over) & (returnVal == err.OK)):
returnVal = err.DRAW
return returnVal
def console_log(level, log_line):
global VERBOSE
if level <= VERBOSE:
print(log_line)
def main():
print("\nTic Tac Toe Platform Test\n_________________________")
g = xo(3, 3, 2, ['X', 'O'], [2, 1])
print(g.get_board())
g.mark(0, 1)
print(g.get_board())
g.mark(0, 2)
print(g.get_board())
g.mark(2, 2)
print(g.get_board())
g.mark(3, 1)
print(g.get_board())
g.mark(4, 2)
print(g.get_board())
print("Game Log:" + str(g.gameLog))
if __name__ == '__main__':
if __doc__:
print('Module:')
print(__doc__ + '\n')
main()