Python Forum
Help with tic-tac-toe project
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with tic-tac-toe project
#4
I restructured the code above. As of programming point of view, it is important to define
helper methods, such as make_move, get_row_winner, get_column_winner etc.

import random

class Tic:
    '''Tic-tac-toe simulator
    '''
    
    board = [[None for j in range(3)]] * 3
    markers = ['X', '0']
    
    def make_move(self, i, j, move_type='X'):
        if i not in [0, 1, 2] or j not in [0, 1, 2]:
            print("Illegal indecies: (%s, %s)" % (i, j))
            return False # be silent or print warning message here!
        if self.board[i][j] is not None:
            print("This position is already marked: (%s, %s)"%(i, j))
            return False # be silent or print warning message here!
        self.board[i][j] = move_type
        return True
    
    def _get_diagonal_winner(self):
        if self.board[0][0] == self.board[1][1] == self.board[2][2]:
            return self.board[0][0]
        if self.board[1][1] == self.board[1][1] == self.board[2][0]:
            return self.board[1][1]
        return None
    
    def _get_row_winner(self):
        for j in range(3):
            if self.board[j][0] == self.board[j][1] == self.board[j][2]:
                return self.board[j][0]
        return None
    
    def _get_column_winner(self):
        self.board = self._transpose(self.board)
        result = self._get_row_winner()
        self.board = self._transpose(self.board)
        return result
    
    @property
    def has_empty_cell(self):
        return any([None in row for row in self.board])
   
    
    @staticmethod
    def _transpose(input_board):
        'Returns transponsed (over main diagonal) board'
        return list(map(list, zip(*input_board))) 
   
    def get_winner(self):
        return self._get_row_winner() or self._get_column_winner() or self._get_diagonal_winner()

    
    def run(self, style='random'):
        ind = 0
        while self.get_winner() is None and self.has_empty_cell:
            i = random.randint(0, 2) if style == 'random' else int(input("Player_%s: Enter i-position of your move (%s):" % (ind % 2, self.markers[ind % 2])))
            j = random.randint(0, 2)  if style == 'random' else int(input("Player_%s: Enter j-position of your move (%s):" % (ind % 2, self.markers[ind % 2])))
            if self.make_move(i, j, self.markers[ind % 2]):
                print('The board state: \n{}\n{}\n{}'.format(self.board[0], self.board[1], self.board[2]))
                ind += 1
        if self.get_winner():
            print('Game over! The winner is Player_%s, marker=%s' % ((ind + 1) % 2, self.get_winner()))
        else:
            print('Sorry... no one wins!')
        print('The board state: \n{}\n{}\n{}'.format(self.board[0], self.board[1], self.board[2]))

if __name__ == '__main__':
    game = Tic()
    game.run('random')
Reply


Messages In This Thread
Help with tic-tac-toe project - by MAZambelli4353 - Aug-15-2018, 01:22 AM
RE: Help with tic-tac-toe project - by woooee - Aug-15-2018, 08:05 AM
RE: Help with tic-tac-toe project - by heras - Aug-15-2018, 03:27 PM
RE: Help with tic-tac-toe project - by scidam - Aug-16-2018, 01:01 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020