In: Computer Science
Design and implement a Python program which will allow two players to play the game of Tic-Tac-Toe in a 4x4 grid!
X | O | X | O
--------------
O | O | X | O
--------------
X | X | O | X
--------------
X | X | O | X
The rules for this game is the same as the classic, 3x3, game –
Requirements
Part A: 20 Points – At a minimum, implement the following three functions –
Part B: 20 Points – Implement logic so that two players can play the game for as many times as they wish to. In addition -
Part C: 5 Points – Enhance your project to include an option for a “player” to play against a “computer” instead
Assumptions
Concepts Utilized in Project
Given data,
Python solution with output screenshot:
Code :
def create_board():
board = [' '] * 16
return board
def display_board(board):
for i in range(16):
if((i+1)%4==0):
print(board[i])
if(i<15):
print("--------------")
else:
print(board[i], end=" | ")
def check_winner(board):
if((board[0] == 'X' and board[1] == 'X' and board[2] == 'X' and board[3] == 'X') or
(board[4] == 'X' and board[5] == 'X' and board[6] == 'X' and board[7] == 'X') or
(board[8] == 'X' and board[9] == 'X' and board[10] == 'X' and board[11] == 'X') or
(board[12] == 'X' and board[13] == 'X' and board[14] == 'X' and board[15] == 'X') or
(board[0] == 'X' and board[4] == 'X' and board[8] == 'X' and board[12] == 'X') or
(board[1] == 'X' and board[5] == 'X' and board[9] == 'X' and board[13] == 'X') or
(board[2] == 'X' and board[6] == 'X' and board[10] == 'X' and board[14] == 'X') or
(board[3] == 'X' and board[7] == 'X' and board[11] == 'X' and board[15] == 'X') or
(board[0] == 'X' and board[5] == 'X' and board[10] == 'X' and board[15] == 'X') or
(board[3] == 'X' and board[6] == 'X' and board[9] == 'X' and board[12] == 'X')):
return 'X'
elif((board[0] == 'O' and board[1] == 'O' and board[2] == 'O' and board[3] == 'O') or
(board[4] == 'O' and board[5] == 'O' and board[6] == 'O' and board[7] == 'O') or
(board[8] == 'O' and board[9] == 'O' and board[10] == 'O' and board[11] == 'O') or
(board[12] == 'O' and board[13] == 'O' and board[14] == 'O' and board[15] == 'O') or
(board[0] == 'O' and board[4] == 'O' and board[8] == 'O' and board[12] == 'O') or
(board[1] == 'O' and board[5] == 'O' and board[9] == 'O' and board[13] == 'O') or
(board[2] == 'O' and board[6] == 'O' and board[10] == 'O' and board[14] == 'O') or
(board[3] == 'O' and board[7] == 'O' and board[11] == 'O' and board[15] == 'O') or
(board[0] == 'O' and board[5] == 'O' and board[10] == 'O' and board[15] == 'O') or
(board[3] == 'O' and board[6] == 'O' and board[9] == 'O' and board[12] == 'O')):
return 'O'
else:
return ''
if __name__ == "__main__":
player1 = 'X'
player2 = 'O'
print("------ Welcome to Tic-Tac-Toe Game -------\n")
print("Player 1 - X")
print("Player 2 - O\n")
player1_name = input("Enter Player 1 Name: ")
player2_name = input("Enter Player 2 Name: ")
print()
total_games = 0
player1_win = 0
player1_losses = 0
player2_win = 0
player2_losses = 0
while(True):
board = create_board()
turn = 1
while(True):
display_board(board)
if(turn == 1):
print("\nPlayer 1 turn, Choose position from (1-16)")
choice = int(input("Choice: "))
board[choice-1] = 'X'
turn = 2
else:
print("\nPlayer 2 turn, Choose position from (1-16)")
choice = int(input("Choice: "))
board[choice-1] = 'O'
turn = 1
winner = check_winner(board)
if(winner == 'X'):
display_board(board)
print("----------------------")
print("\nGame Over")
print("** Player 1 Won **")
player1_win += 1
player2_losses += 1
total_games += 1
break
elif(winner == 'O'):
display_board(board)
print("\nGame Over")
print("** Player 2 Won **")
player2_win += 1
player1_losses += 1
total_games += 1
break
else:
if(' ' in board):
continue
display_board(board)
print("\nGame Over")
print("** Draw **")
total_games += 1
break
print("\nDo you want to play again? (yes/no): ")
again = input()
if(again == 'yes' or again == "Yes" or again == "YES"):
continue
else:
break
print("\nTotal Games Played:", total_games)
print("------------------------")
print(player1_name, "(Player 1) wins:", player1_win)
print(player1_name, "(Player 1) losses:", player1_losses)
print(player1_name, "(Player 2) wins:", player2_win)
print(player1_name, "(Player 2) looses:", player2_losses)
Output :
I have not included whole screenshot because it is too long
Note : for better formatting please refer to the code formatting below.
<<<<Please Give Me Like>>>>