class Board(object): def __init__(self): self.board = [0,1,2,3,4,5,6,7,8,9] ## board[0] denotes whoes turn it is def putMarker(self,player,move): if self.board[move].isalpha(): return False if self.board[move].isdigit(): self.board[move] = player return True def isFull (self): for i in range (1,10): if str(self.board[i]).isdigit(): return False return True def score(self): if cell[0] == cell[1] == cell[2] == 'O' or cell[3] == cell[4] == cell[5] == 'O' or cell[6] == cell[7] == cell[8] == 'O' or cell[0] == cell[3] == cell[6] == 'O' or cell[1] == cell[4] == cell[7] == 'O' or cell[2] == cell[5] == cell[8]== 'O': return "The player with O's wins" if cell[0] == cell[1] == cell[2] == 'X' or cell[3] == cell[4] == cell[5] == 'X' or cell[6] == cell[7] == cell[8] == 'X' or cell[0] == cell[3] == cell[6] == 'X' or cell[1] == cell[4] == cell[7] == 'X' or cell[2] == cell[5] == cell[8]== 'X': return "The player with X's wins" elif isFull: return "Tie" else: return False def display(self): return self.board[1] + "|" + str(self.board[2]) + "|" + str(self.board[3]) + "\n" +"-+-+-\n" +str(self.board[4]) + "|" + str(self.board[5]) + "|" + str(self.board[6]) + "\n" +"-+-+-\n"+str(self.board[7]) + "|" + str(self.board[8]) + "|" + str(self.board[9]) ##class Player(object): ## def __init__(self,name): ## self.name = name ## self.shape = shape class HumanPlayer(object): def __init__(self): self.name = name self.MyScore = 0 def name(self): return self.name def getNextMove (self, board): move = input("it is your move" + self.name + "where do you want to play?") if board.putMarker(): board.putMarker(name,move) def PlayGame(p1,p2): TheBoard = Board() while TheBoard.score == False: print TheBoard.display if TheBoard.board[0] == 0 and TheBoard.isFull == False: p1.getNextMove(TheBoard) TheBoard.board[0] = 1 if TheBoard.board[0] == 1 and TheBoard.isFull == False: p2.getNextMove(TheBoard) TheBoard.board[0] = 0 ##Display board.. ##Ask for new move.. ##Execute move, if false skip turn.. ##alternate turn PlayGame('p1','p2')
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question