Aug-15-2018, 01:22 AM
(This post was last modified: Aug-15-2018, 02:00 AM by MAZambelli4353.)
Hello Ive been teaching myself python for almost a month now and Im trying to implement classes into this project which is something Im new to. Its not finished but Im getting a wrong output when I try to determine if someone won. I sometimes get a random 'You won'(sometimes 2-4 of them) even though there aren't three in a row of the same letter. I feel like I dont understand how classes work anymore.
Just figured out it was the if statements and that the 'ands' didnt pool all of the other coordinates, only the last one. Anyway, any comments or advice to improve my code would be appreciated!- thanks
import random class Tic: board=[[' ',' ',' '],[' ',' ',' '],[' ',' ',' ']] placement=[[0,0],[0,1],[0,2],[1,0],[1,1],[1,2],[2,0],[2,1],[2,2]] def __init__(self): pass def print_board(self): print('{}\n{}\n{}'.format(self.board[0],self.board[1],self.board[2])) def change_board(self,row,column): if player1 is self: self.board[row][column]='X' if comp is self: self.board[row][column]='O' def did_someone_win(self,letter): if self.board[0][0] and self.board[1][0] and self.board[2][0] == letter: print('You won!') if self.board[0][1] and self.board[1][1] and self.board[2][1] == letter: print('You won!') if self.board[0][2] and self.board[1][2] and self.board[2][2] == letter: print('You won!') if self.board[0][0] and self.board[0][1] and self.board[0][2] == letter: print('You won!') if self.board[1][0] and self.board[1][1] and self.board[1][2] == letter: print('You won!') if self.board[2][0] and self.board[2][1] and self.board[2][2] == letter: print('You won!') if self.board[0][0] and self.board[1][1] and self.board[2][2] == letter: print('You won!') if self.board[2][0] and self.board[1][1] and self.board[0][2] == letter: print('You won!') else: pass game=Tic();player1=Tic();comp=Tic() print('First move will be randomly chosen. Enter s to start game: ') starting_key=input('') if starting_key is 's': start_game=True else: start_game=False player1_move=False;comp_move=False if random.randint(0, 1) == 1: print('Player1 goes first.') player1_move = True else: print('Computer goes first.') comp_move = True while start_game: while player1_move: print('Please enter one of the possible coordinates: ',game.placement) #shows available coordinates var1, var2 = input("Enter your coordinates separated by a space here: ").split()#enter coordinates var1=int(var1);var2=int(var2)#changes inputs into int's to avoid error player1.change_board(var1,var2)#using class method to change board array game.print_board()#using method to print the board with new inputs game.placement.pop(game.placement.index([var1,var2]))#removes coordinates already used. game.did_someone_win('X')#This is what doesnt work as I get 'You won' randomly sometimes. comp_move=True player1_move=False while comp_move: print('Computers move.') comp_choice = random.choice(game.placement)#randomly chooses computer's choice comp.change_board(comp_choice[0],comp_choice[1])#using class method to change board array game.placement.pop(game.placement.index([comp_choice[0], comp_choice[1]]))#remove used coordinates game.print_board() print(game.placement) game.did_someone_win('O')#Again doesnt work properly cant figure out why player1_move = True comp_move=False /python] [hr] [python] import random class Tic: board=[[' ',' ',' '],[' ',' ',' '],[' ',' ',' ']] placement=[[0,0],[0,1],[0,2],[1,0],[1,1],[1,2],[2,0],[2,1],[2,2]] def __init__(self): pass def print_board(self): print('{}\n{}\n{}'.format(self.board[0],self.board[1],self.board[2])) def change_board(self,row,column): if player1 is self: self.board[row][column]='X' if comp is self: self.board[row][column]='O' def did_someone_win(self,letter): if self.board[0][0] and self.board[1][0] and self.board[2][0] == letter: print('You won!') if self.board[0][1] and self.board[1][1] and self.board[2][1] == letter: print('You won!') if self.board[0][2] and self.board[1][2] and self.board[2][2] == letter: print('You won!') if self.board[0][0] and self.board[0][1] and self.board[0][2] == letter: print('You won!') if self.board[1][0] and self.board[1][1] and self.board[1][2] == letter: print('You won!') if self.board[2][0] and self.board[2][1] and self.board[2][2] == letter: print('You won!') if self.board[0][0] and self.board[1][1] and self.board[2][2] == letter: print('You won!') if self.board[2][0] and self.board[1][1] and self.board[0][2] == letter: print('You won!') else: pass game=Tic();player1=Tic();comp=Tic() print('First move will be randomly chosen. Enter s to start game: ') starting_key=input('') if starting_key is 's': start_game=True else: start_game=False player1_move=False;comp_move=False if random.randint(0, 1) == 1: print('Player1 goes first.') player1_move = True else: print('Computer goes first.') comp_move = True while start_game: while player1_move: print('Please enter one of the possible coordinates: ',game.placement) #shows available coordinates var1, var2 = input("Enter your coordinates separated by a space here: ").split()#enter coordinates var1=int(var1);var2=int(var2)#changes inputs into int's to avoid error player1.change_board(var1,var2)#using class method to change board array game.print_board()#using method to print the board with new inputs game.placement.pop(game.placement.index([var1,var2]))#removes coordinates already used. game.did_someone_win('X')#This is what doesnt work as I get 'You won' randomly sometimes. comp_move=True player1_move=False while comp_move: print('Computers move.') comp_choice = random.choice(game.placement)#randomly chooses computer's choice comp.change_board(comp_choice[0],comp_choice[1])#using class method to change board array game.placement.pop(game.placement.index([comp_choice[0], comp_choice[1]]))#remove used coordinates game.print_board() print(game.placement) game.did_someone_win('O')#Again doesnt work properly cant figure out why player1_move = True comp_move=False
Just figured out it was the if statements and that the 'ands' didnt pool all of the other coordinates, only the last one. Anyway, any comments or advice to improve my code would be appreciated!- thanks