Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- gameRunning = True
- class Player:
- name = ""
- state = 0b000000000
- def __init__(self,playerCharacter):
- self.name = playerCharacter
- class Board:
- winStates = [0b111000000,0b000111000,0b000000111,0b100100100,0b010010010,0b001001001,0b100010001,0b001010100]
- boardArray = ["1","2","3","4","5","6","7","8","9"]
- def __init__(self,players):
- self.players = players
- def printBoard(self):
- print (self.boardArray[0] + "|" + self.boardArray[1] + "|" + self.boardArray[2])
- print ("-----")
- print (self.boardArray[3] + "|" + self.boardArray[4] + "|" + self.boardArray[5])
- print ("-----")
- print (self.boardArray[6] + "|" + self.boardArray[7] + "|" + self.boardArray[8])
- def choose(self,player,locationNumber):
- if self.validChoice(locationNumber):
- self.boardArray[int(locationNumber)-1] = player.name
- player.state |= 1 << int(locationNumber) - 1
- return True
- else:
- return False
- def boardTaken(self):
- result = 0
- for player in self.players:
- result |= player.state
- return result
- def validChoice(self,locationNumber):
- choice = 1 << int(locationNumber)
- bitboard = self.boardTaken()
- if (choice & bitboard) == 0:
- return True
- else:
- return False
- #Game
- board = Board([Player("X"),Player("O")])
- turn = 0;
- # loop
- while gameRunning:
- print("-------------------")
- board.printBoard()
- print ("{0} turn".format(board.players[turn % len(board.players)].name))
- print ("select a number or type e to exit")
- s = input()
- if(s == "e"):
- gameRunning = False
- break
- if int(s) >= 1 and int(s) <=9 and board.choose(board.players[turn % len(board.players)],s):
- turn += 1
- else:
- print(" V V V V That is not a valid choice please choose a valid location")
- for wins in board.winStates:
- for player in board.players:
- check = player.state & wins
- if check == wins:
- print("Player {} won!".format(player.name))
- exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement