Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TicTacToe Logic
#1
Hello Folks,
I Made Simple TicTacToe, But It seems there is something missd, can any one help me with this, i want to know what i miss as computational thinking.
and here is my GitHub and my code .

https://github.com/AhmadMWaddah/TicTacToeAMW.git

Code:
tic_brd = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']


# Check Board Cells.
def chk_brd(brd):
    while brd[0] == ' ' or brd[1] == ' ' or brd[2] == ' ' or brd[3] == ' ' or brd[4] == ' ' or brd[5] == ' ' or brd[6] == ' ' or brd[7] == ' ' or brd[8] == ' ':
        print('Board Is Not Full.')

        def board_size(cells):
            print(f'||~~~~~||~~~~~||~~~~~||')
            print(f'||  {cells[0]}  ||  {cells[1]}  ||  {cells[2]}  ||')
            print(f'||~~~~~||~~~~~||~~~~~||')
            print(f'||  {cells[3]}  ||  {cells[4]}  ||  {cells[5]}  ||')
            print(f'||~~~~~||~~~~~||~~~~~||')
            print(f'||  {cells[6]}  ||  {cells[7]}  ||  {cells[8]}  ||')
            print(f'||~~~~~||~~~~~||~~~~~||')

        board_size(tic_brd)

        def player_input():
            position_one = ''
            while (position_one.upper() != 'X') or (position_one.upper() != 'O'):
                position_one = input(' Please Enter "X" Or "O".: ').upper()
                if position_one.upper() == 'X':
                    print(f' Player One {position_one}.')
                    break
                elif position_one.upper() == 'O':
                    print(f' Player One {position_one}.')
                    break

            def place_marker(board):
                if position_one:
                    cel = int(input(' Enter Cell: '))
                    while cel < 0 or cel > 9:
                        cel = int(input(f' Cell {cel} Is Out Of Range ( 0 - 8 ): '))
                    else:
                        while board[cel] != ' ':
                            cel = int(input(f' Cell {cel} Is Full Enter Another Cell: '))
                            continue
                        else:
                            board[cel] = position_one

            board_size(tic_brd)

            # Check Win
            def win_check(brdd, pos_on):
                while (brdd[0] == pos_on and brdd[1] == pos_on and brdd[2] == pos_on) or (
                        brdd[3] == pos_on and brdd[4] == pos_on and brdd[5] == pos_on) or (
                        brdd[6] == pos_on and brdd[7] == pos_on and brdd[8] == pos_on) or (
                        brdd[0] == pos_on and brdd[3] == pos_on and brdd[6] == pos_on) or (
                        brdd[1] == pos_on and brdd[4] == pos_on and brdd[7] == pos_on) or (
                        brdd[2] == pos_on and brdd[5] == pos_on and brdd[8] == pos_on) or (
                        brdd[0] == pos_on and brdd[5] == pos_on and brdd[8] == pos_on) or (
                        brdd[2] == pos_on and brdd[4] == pos_on and brdd[6] == pos_on):
                    print(f'Game Over. {pos_on} Win.')
                else:
                    place_marker(tic_brd)
                    board_size(tic_brd)

            win_check(tic_brd, position_one)

        player_input()
    else:
        print(f'Yes, Board Is Full. Game Over. ')


chk_brd(tic_brd)
Thank You In Advance For Support,
Smile Smile Smile
Reply
#2
I recommend you refactor this so that you don't define your functions in loops. Just define them once, at the top, since you shouldn't do things in loops that only need to happen once.
Reply
#3
Thanks Buddy, I will Rules Too Before Post Again,
Reply


Forum Jump:

User Panel Messages

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