0% found this document useful (0 votes)
18 views16 pages

LAB06 Ai

This document describes the code for a Tic-Tac-Toe game. It includes functions for initializing the board, checking for a win or draw, and playing a single game. The code allows two players to compete in a series of Tic-Tac-Toe matches to determine an overall winner.

Uploaded by

gocev58088
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views16 pages

LAB06 Ai

This document describes the code for a Tic-Tac-Toe game. It includes functions for initializing the board, checking for a win or draw, and playing a single game. The code allows two players to compete in a series of Tic-Tac-Toe matches to determine an overall winner.

Uploaded by

gocev58088
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Artificial Intelligence (CS-316L) LAB # 06

LAB # 06
Tasks 1:
import os
import time

board = [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ']
player = 1

Win = 1
Draw = -1
Running = 0
Stop = 1

Game = Running
Mark = 'X'

def DrawBoard():
print(" %c | %c | %c " % (board[1],board[2],board[3]))
print("___|___|___")
print(" %c | %c | %c " % (board[4],board[5],board[6]))
print("___|___|___")
print(" %c | %c | %c " % (board[7],board[8],board[9]))

#This Function Checks position is empty or not

Sir Syed University of Engineering and Technology ROLL NO # 215

(Department of Computer Science and Information Technology)


Artificial Intelligence (CS-316L) LAB # 06

def CheckPosition(x):
if(board[x] == ' '):
return True
else:
return False

def CheckWin():
global Game
#Horizontal winning condition
if(board[1] == board[2] and board[2] == board[3] and board[1] != ' '):
Game = Win
elif(board[4] == board[5] and board[5] == board[6] and board[4] != ' '):
Game = Win
elif(board[7] == board[8] and board[8] == board[9] and board[7] != ' '):
Game = Win
#Vertical Winning Condition
elif(board[1] == board[4] and board[4] == board[7] and board[1] != ' '):
Game = Win
elif(board[2] == board[5] and board[5] == board[8] and board[2] != ' '):
Game = Win
elif(board[3] == board[6] and board[6] == board[9] and board[3] != ' '):
Game=Win
#Diagonal Winning Condition
elif(board[1] == board[5] and board[5] == board[9] and board[5] != ' '):

Sir Syed University of Engineering and Technology ROLL NO # 215

(Department of Computer Science and Information Technology)


Artificial Intelligence (CS-316L) LAB # 06

Game = Win
elif(board[3] == board[5] and board[5] == board[7] and board[5] != ' '):
Game=Win
#Match Tie or Draw Condition
elif(board[1]!=' ' and board[2]!=' ' and board[3]!=' ' and board[4]!=' ' and board[5]!=' ' and
board[6]!=' ' and board[7]!=' ' and board[8]!=' ' and board[9]!=' '):
Game=Draw
else:
Game=Running

print("Tic-Tac-Toe")
print("Abdul Rehman [X] --- Taha [O]\n")
print()

while(Game == Running):
os.system('cls')
DrawBoard()
if(player % 2 != 0):
print("AR's chance")
Mark = 'X'
else:
print("TAHA's chance")
Mark = 'O'
choice = int(input("Enter the position between [1-9] where you want to mark : "))
if(CheckPosition(choice)):
board[choice] = Mark

Sir Syed University of Engineering and Technology ROLL NO # 215

(Department of Computer Science and Information Technology)


Artificial Intelligence (CS-316L) LAB # 06

player+=1
CheckWin()

os.system('cls')
DrawBoard()
if(Game==Draw):
print("Game Draw")
elif(Game==Win):
player-=1
print("\n \t________________")
if(player%2!=0):
print("\t AR Won")
else:
print("\t TAHA Won")
print(" \t________________")

OUTPUT:
Tic-Tac-Toe
Abdul Rehman [X] --- Taha [O]

| |
___|___|___
| |
___|___|___
| |
AR's chance
Enter the position between [1-9] where you want to mark : 2
| X |
___|___|___

Sir Syed University of Engineering and Technology ROLL NO # 215

(Department of Computer Science and Information Technology)


Artificial Intelligence (CS-316L) LAB # 06

| |
___|___|___
| |
TAHA's chance
Enter the position between [1-9] where you want to mark : 1
O | X |
___|___|___
| |
___|___|___
| |
AR's chance
Enter the position between [1-9] where you want to mark : 3
O | X | X
___|___|___
| |
___|___|___
| |
TAHA's chance
Enter the position between [1-9] where you want to mark : 4
O | X | X
___|___|___
O | |
___|___|___
| |
AR's chance
Enter the position between [1-9] where you want to mark : 7
O | X | X
___|___|___
O | |
___|___|___
X | |
TAHA's chance
Enter the position between [1-9] where you want to mark : 9
O | X | X
___|___|___
O | |
___|___|___
X | | O
AR's chance
Enter the position between [1-9] where you want to mark : 8

Sir Syed University of Engineering and Technology ROLL NO # 215

(Department of Computer Science and Information Technology)


Artificial Intelligence (CS-316L) LAB # 06

O | X | X
___|___|___
O | |
___|___|___
X | X | O
TAHA's chance
Enter the position between [1-9] where you want to mark : 5
O | X | X
___|___|___
O | O |
___|___|___
X | X | O

________________
TAHA Won
________________

Tasks 2:
def print_tic_tac_toe(values):
print("\n")
print("\t | | |")
print("\t {} | {} | {} | {}".format(values[0], values[1],
values[2], values[3]))
print('\t_____|_____|_____|_____')

print("\t | | |")
print("\t {} | {} | {} | {}".format(values[4], values[5],
values[6], values[7]))
print('\t_____|_____|_____|_____')

print("\t | | |")
print("\t {} | {} | {} | {}".format(values[8], values[9],
values[10], values[11]))
print('\t_____|_____|_____|_____')

print("\t | | |")

print("\t {} | {} | {} | {}".format(values[12], values[13],


values[14], values[15]))

Sir Syed University of Engineering and Technology ROLL NO # 215

(Department of Computer Science and Information Technology)


Artificial Intelligence (CS-316L) LAB # 06

print("\t | | |")
print("\n")

# Function to print the score-board


def print_scoreboard(score_board):
print("\t--------------------------------")
print("\t SCOREBOARD ")
print("\t--------------------------------")

players = list(score_board.keys())
print("\t ", players[0], "\t ", score_board[players[0]])
print("\t ", players[1], "\t ", score_board[players[1]])

print("\t--------------------------------\n")

# Function to check if any player has won


def check_win(player_pos, cur_player):

# All possible winning combinations


soln = [[1, 2, 3,4], [ 5, 6,7,8], [9, 10, 11,12],[13,14,15,16],
[1,5,9,13],[2,6,10,14],[3,7,11,15],[4,8,12,16], [1, 6,11, 16],
[4,7,10,13]]

# Loop to check if any winning combination is satisfied


for x in soln:
if all(y in player_pos[cur_player] for y in x):

# Return True if any winning combination satisfies


return True
# Return False if no combination is satisfied
return False

# Function to check if the game is drawn


def check_draw(player_pos):
if len(player_pos['X']) + len(player_pos['O']) == 16:
return True
return False

# Function for a single game of Tic Tac Toe

Sir Syed University of Engineering and Technology ROLL NO # 215

(Department of Computer Science and Information Technology)


Artificial Intelligence (CS-316L) LAB # 06

def single_game(cur_player):

# Represents the Tic Tac Toe


values = [' ' for x in range(16)]

# Stores the positions occupied by X and O


player_pos = {'X':[], 'O':[]}

# Game Loop for a single game of Tic Tac Toe


while True:
print_tic_tac_toe(values)

# Try exception block for MOVE input


try:
print("Player ", cur_player, " turn. Which box? : ", end="")
move = int(input())
except ValueError:
print("Wrong Input!!! Try Again")
continue

# Sanity check for MOVE inout


if move < 1 or move > 16:
print("Wrong Input!!! Try Again")
continue

# Check if the box is not occupied already


if values[move-1] != ' ':
print("Place already filled. Try again!!")
continue

# Update game information

# Updating grid status


values[move-1] = cur_player

# Updating player positions


player_pos[cur_player].append(move)

# Function call for checking win


if check_win(player_pos, cur_player):

Sir Syed University of Engineering and Technology ROLL NO # 215

(Department of Computer Science and Information Technology)


Artificial Intelligence (CS-316L) LAB # 06

print_tic_tac_toe(values)
print("Player ", cur_player, " has won the game!!")
print("\n")
return cur_player

# Function call for checking draw game


if check_draw(player_pos):
print_tic_tac_toe(values)
print("Game Drawn")
print("\n")
return 'D'

# Switch player moves


if cur_player == 'X':
cur_player = 'O'
else:
cur_player = 'X'

if __name__ == "__main__":

print("Player 1")
player1 = input("Enter the name : ")
print("\n")

print("Player 2")
player2 = input("Enter the name : ")
print("\n")

# Stores the player who chooses X and O


cur_player = player1

# Stores the choice of players


player_choice = {'X' : "", 'O' : ""}

# Stores the options


options = ['X', 'O']

# Stores the scoreboard


score_board = {player1: 0, player2: 0}
print_scoreboard(score_board)

Sir Syed University of Engineering and Technology ROLL NO # 215

(Department of Computer Science and Information Technology)


Artificial Intelligence (CS-316L) LAB # 06

# Game Loop for a series of Tic Tac Toe


# The loop runs until the players quit
while True:

# Player choice Menu


print("Turn to choose for", cur_player)
print("Enter 1 for X")
print("Enter 2 for O")
print("Enter 3 to Quit")

# Try exception for CHOICE input


try:
choice = int(input())
except ValueError:
print("Wrong Input!!! Try Again\n")
continue

# Conditions for player choice


if choice == 1:
player_choice['X'] = cur_player
if cur_player == player1:
player_choice['O'] = player2
else:
player_choice['O'] = player1

elif choice == 2:
player_choice['O'] = cur_player
if cur_player == player1:
player_choice['X'] = player2
else:
player_choice['X'] = player1

elif choice == 3:
print("Final Scores")
print_scoreboard(score_board)
break

else:
print("Wrong Choice!!!! Try Again\n")

Sir Syed University of Engineering and Technology ROLL NO # 215

(Department of Computer Science and Information Technology)


Artificial Intelligence (CS-316L) LAB # 06

# Stores the winner in a single game of Tic Tac Toe


winner = single_game(options[choice-1])

# Edits the scoreboard according to the winner


if winner != 'D' :
player_won = player_choice[winner]
score_board[player_won] = score_board[player_won] + 1

print_scoreboard(score_board)
# Switch player who chooses X or O
if cur_player == player1:
cur_player = player2
else:
cur_player = player1
OUTPUT:
Player 1
Enter the name : ABDUL REHMAN

Player 2
Enter the name : TAHA

--------------------------------
SCOREBOARD
--------------------------------
ABDUL REHMAN 0
TAHA 0
--------------------------------

Turn to choose for ABDUL REHMAN


Enter 1 for X
Enter 2 for O
Enter 3 to Quit
1

| | |

Sir Syed University of Engineering and Technology ROLL NO # 215

(Department of Computer Science and Information Technology)


Artificial Intelligence (CS-316L) LAB # 06

| | |
_____|_____|_____|_____
| | |
| | |
_____|_____|_____|_____
| | |
| | |
_____|_____|_____|_____
| | |
| | |
| | |

Player X turn. Which box? : 9

| | |
| | |
_____|_____|_____|_____
| | |
| | |
_____|_____|_____|_____
| | |
X | | |
_____|_____|_____|_____
| | |
| | |
| | |

Player O turn. Which box? : 4

| | |
| | | O
_____|_____|_____|_____
| | |
| | |
_____|_____|_____|_____
| | |

Sir Syed University of Engineering and Technology ROLL NO # 215

(Department of Computer Science and Information Technology)


Artificial Intelligence (CS-316L) LAB # 06

X | | |
_____|_____|_____|_____
| | |
| | |
| | |

Player X turn. Which box? : 10

| | |
| | | O
_____|_____|_____|_____
| | |
| | |
_____|_____|_____|_____
| | |
X | X | |
_____|_____|_____|_____
| | |
| | |
| | |

Player O turn. Which box? : 6

| | |
| | | O
_____|_____|_____|_____
| | |
| O | |
_____|_____|_____|_____
| | |
X | X | |
_____|_____|_____|_____
| | |
| | |
| | |

Sir Syed University of Engineering and Technology ROLL NO # 215

(Department of Computer Science and Information Technology)


Artificial Intelligence (CS-316L) LAB # 06

Player X turn. Which box? : 11

| | |
| | | O
_____|_____|_____|_____
| | |
| O | |
_____|_____|_____|_____
| | |
X | X | X |
_____|_____|_____|_____
| | |
| | |
| | |

Player O turn. Which box? : 7

| | |
| | | O
_____|_____|_____|_____
| | |
| O | O |
_____|_____|_____|_____
| | |
X | X | X |
_____|_____|_____|_____
| | |
| | |
| | |

Player X turn. Which box? : 13

| | |
| | | O

Sir Syed University of Engineering and Technology ROLL NO # 215

(Department of Computer Science and Information Technology)


Artificial Intelligence (CS-316L) LAB # 06

_____|_____|_____|_____
| | |
| O | O |
_____|_____|_____|_____
| | |
X | X | X |
_____|_____|_____|_____
| | |
X | | |
| | |

Player O turn. Which box? : 8

| | |
| | | O
_____|_____|_____|_____
| | |
| O | O | O
_____|_____|_____|_____
| | |
X | X | X |
_____|_____|_____|_____
| | |
X | | |
| | |

Player X turn. Which box? : 12

| | |
| | | O
_____|_____|_____|_____
| | |
| O | O | O
_____|_____|_____|_____
| | |
X | X | X | X

Sir Syed University of Engineering and Technology ROLL NO # 215

(Department of Computer Science and Information Technology)


Artificial Intelligence (CS-316L) LAB # 06

_____|_____|_____|_____
| | |
X | | |
| | |

Player X has won the game!!

--------------------------------
SCOREBOARD
--------------------------------
ABDUL REHMAN 1
TAHA 0
--------------------------------

Turn to choose for TAHA


Enter 1 for X
Enter 2 for O
Enter 3 to Quit

Sir Syed University of Engineering and Technology ROLL NO # 215

(Department of Computer Science and Information Technology)

You might also like