Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy
- a1= a2= a3= b1= b2= b3= c1= c2= c3 = "+"
- board = [[a1, a2, a3],
- [b1, b2, b3],
- [c1, c2, c3]]
- wins = [['a1', 'a2', 'a3'],
- ['b1', 'b2', 'b3'],
- ['c1', 'c2', 'c3'],
- ['a1', 'b2', 'c3'],
- ['c1', 'b2', 'a3'],
- ['a1', 'b1', 'c1'],
- ['a2', 'b2', 'c2'],
- ['a3', 'b3', 'c3']]
- class Player:
- def __init__(self, bool):
- self.moves = self.marker = self.player = 0
- self.pegs = []
- self.won = False
- if bool==1:
- self.player=2
- self.marker="O"
- else:
- self.player=1
- self.marker="X"
- def peg(self, coord, board):
- for y,letter in enumerate(['a','b','c']):
- for x,number in enumerate(['1','2','3']):
- if (coord[0] == letter) and (coord[1] == number):
- board[y][x] = self.marker
- self.pegs.append(("%s%s" % (letter,number)))
- #exec("%s='%s'" % (coord, self.marker))
- self.moves += 1
- return board;
- def checkWinner(self):
- for win in wins:
- score = 0
- for peg in self.pegs:
- for i in [0,1,2]:
- if peg == win[i]:
- score += 1
- if score >= 3:
- self.won = True
- break
- return self.won;
- def gameBoard ():
- print " 1 2 3"
- print "a {0} | {1} | {2}".format(*board[0])
- print " ----------"
- print "b {0} | {1} | {2}".format(*board[1])
- print " ----------"
- print "c {0} | {1} | {2}".format(*board[2])
- t = "go"
- p1 = Player(0)
- p2 = Player(1)
- players = [p1, p2]
- turn = 0
- while t == "go":
- move = (turn % 2)
- gameBoard()
- coord = raw_input("Input a coordinate, player %s > " % (players[move].player))
- players[move].peg(coord, board)
- if (players[move].checkWinner() == False):
- turn += 1
- else:
- print("player%s wins!!" % (players[move].player))
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement