Answer to Question #255813 in Python for Chethan

Question #255813
Input The input will be three lines contain O's and X's separated by space. Output The output should be a single line containing either "Abhinav Wins" or "Anjali Wins" or "Tie". Explanation For example, if the input is O X O O X X O O X as three of O's are in vertical row print "Abhinav Wins". Sample Input 1 O X O O X X O O X Sample Output 1 Abhinav Wins Sample Input 2 O O X X X O X O O Sample Output 2 Anjali Wins
1
Expert's answer
2021-10-30T00:40:38-0400
import random
class Game:
    def __init__(S):
        S.field = []
    def create_field(S):
        for i in range(3):
            R = []
            for j in range(3):
                R.append('-')
            S.field .append(R)
    def get_first(S):
        return random.randint(0, 1)


    def fix_spot(S, R, C, player):
        S.field [R][C] = player


    def is_player_win(S, player):
        win = None


        n = len(S.field )
        for i in range(n):
            win = True
            for j in range(n):
                if S.field [i][j] != player:
                    win = False
                    break
            if win:
                return win
        for i in range(n):
            win = True
            for j in range(n):
                if S.field [j][i] != player:
                    win = False
                    break
            if win:
                return win
        win = True
        for i in range(n):
            if S.field [i][i] != player:
                win = False
                break
        if win:
            return win


        win = True
        for i in range(n):
            if S.field [i][n - 1 - i] != player:
                win = False
                break
        if win:
            return win
        return False


        for R in S.field:
            for item in R:
                if item == '-':
                    return False
        return True


    def is_field_filled(S):
        for R in S.field:
            for item in R:
                if item == '-':
                    return False
        return True


    def swap_player(S, player):
        return 'X' if player == 'O' else 'O'


    def show_field(S):
        for R in S.field:
            for item in R:
                print(item, end=" ")
            print()


    def start(S):
        S.create_field()


        player = 'X' if S.get_first() == 1 else 'O'
        while True:
            print(f"Player {player} turn")


            S.show_field()
            R, col = list(
                map(int, input("Enter row and column: ").split()))
            print()
            S.fix_spot(R - 1, col - 1, player)
            if S.is_player_win(player):
                print(f"Player {player} wins the game!")
                print(" Abhinav Wins")
                break
            if S.is_field_filled():
                print("Match Draw!")
                break
            player = S.swap_player(player)
        print()
        S.show_field()
gam = Game()
gam.start()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS