|
| 1 | +def Welcome(): |
| 2 | + print(' *Welcome to tic-tac-toe game*') |
| 3 | + #dont take sleep more than 2 seconds then user will get bored |
| 4 | + sleep(1) |
| 5 | + print() |
| 6 | + print('Computer will decide who will play first') |
| 7 | + print() |
| 8 | + #for Hint Feature In Computer AI i have passed Player letter instead of computer letter |
| 9 | + print('If you need Hint in the middle of game \npress any of this [Hint,hint,H,h]') |
| 10 | + sleep(1) |
| 11 | + print() |
| 12 | + print(''' *** Format of Game ** |
| 13 | + | | 1 | 2 | 3 |
| 14 | + - - - - - - - - - - - - |
| 15 | + | | 4 | 5 | 6 |
| 16 | + - - - - - - - - - - - - |
| 17 | + | | 7 | 8 | 9 |
| 18 | + ''') |
| 19 | + |
| 20 | + |
| 21 | +#Fuction to draw Board |
| 22 | +#you can modify this sleep method if you dont need it |
| 23 | +def DrawBoard(board,NeedSleep=True): |
| 24 | + #I thought for hint u dont need sleep method so i given default value for Needsleep |
| 25 | + if NeedSleep: |
| 26 | + sleep(1) |
| 27 | + print() |
| 28 | + print(' '+board[1]+' | '+board[2]+' | '+board[3]) |
| 29 | + print(' - - - - - - - ') |
| 30 | + print(' '+board[4]+' | '+board[5]+' | '+board[6]) |
| 31 | + print(' - - - - - - - ') |
| 32 | + print(' '+board[7]+' | '+board[8]+' | '+board[9]) |
| 33 | + print() |
| 34 | + |
| 35 | +#asking the player to choose thier Letter (X or O) |
| 36 | +def InputPlayerLetter(): |
| 37 | + letter='' |
| 38 | + #Ask untill user enters x or o |
| 39 | + while not(letter == 'X' or letter == 'O'): |
| 40 | + print('Do you want to be X or O') |
| 41 | + letter = input().upper() |
| 42 | + |
| 43 | + #returning list bcz of later use |
| 44 | + if letter == 'X': |
| 45 | + return ['X','O'] |
| 46 | + if letter == 'O': |
| 47 | + return ['O','X'] |
| 48 | + |
| 49 | +#Deciding who should play first randomly |
| 50 | +#in usual tic-tac-toe games player first,but it selects randomly between computer and player |
| 51 | +def WhoFirst(): |
| 52 | + opponents = ['computer','player'] |
| 53 | + if choice(opponents) == 'computer': |
| 54 | + return 'computer' |
| 55 | + else : |
| 56 | + return 'player' |
| 57 | + |
| 58 | +#this function asks player to play again |
| 59 | +def PlayAgain(): |
| 60 | + print() |
| 61 | + print('Do you want to Play Again (y or n)') |
| 62 | + playagain = input().lower().startswith('y') |
| 63 | + return playagain |
| 64 | + |
| 65 | +#function for making move |
| 66 | +def MakeMove(board,letter,move): |
| 67 | + board[move] = letter |
| 68 | + |
| 69 | +#check if any one win,returns True if wins |
| 70 | +#In tic-tac-toe there are 8 possibilities of winning |
| 71 | +#horizontal => 3 | vertical => 3 | diagonal => 2 |
| 72 | +def IsWinner(board,letter): |
| 73 | + return ( (board[7] == letter and board[8] == letter and board[9] == letter ) or |
| 74 | + (board[4] == letter and board[5] == letter and board[6] == letter ) or |
| 75 | + (board[1] == letter and board[2] == letter and board[3] == letter ) or |
| 76 | + (board[1] == letter and board[4] == letter and board[7] == letter ) or |
| 77 | + (board[2] == letter and board[5] == letter and board[8] == letter ) or |
| 78 | + (board[3] == letter and board[6] == letter and board[9] == letter ) or |
| 79 | + (board[1] == letter and board[5] == letter and board[9] == letter ) or |
| 80 | + (board[3] == letter and board[5] == letter and board[7] == letter ) ) |
| 81 | + |
| 82 | +#duplicate board is useful when we wanted to make temporary changes to the temporary copy of the board without changing the original board |
| 83 | +def GetBoardCopy(board): |
| 84 | + DupeBoard = [] |
| 85 | + for i in board: |
| 86 | + DupeBoard.append(i) |
| 87 | + return DupeBoard |
| 88 | + |
| 89 | +#fuction to check is space is free |
| 90 | +def IsSpaceFree(board,move): |
| 91 | + return board[move] == ' ' |
| 92 | + |
| 93 | +#Getting the player move |
| 94 | +def GetPlayerMove(board): |
| 95 | + move = '' |
| 96 | + |
| 97 | + while move not in '1 2 3 4 5 6 7 8 9'.split() or not IsSpaceFree(board,int(move)): |
| 98 | + print() |
| 99 | + print('Enter your move (1 - 9)') |
| 100 | + move = input() |
| 101 | + #if player wants Hint |
| 102 | + if move in HintInput: |
| 103 | + return move |
| 104 | + break |
| 105 | + return int(move) |
| 106 | + |
| 107 | +#choose random move from given list |
| 108 | +#it is used when AI wants to choose out of many possibilities |
| 109 | +def ChooseRandomFromList(board,MoveList): |
| 110 | + PossibleMoves = [] |
| 111 | + for i in MoveList: |
| 112 | + if IsSpaceFree(board,i): |
| 113 | + PossibleMoves.append(i) |
| 114 | + if len(PossibleMoves) != 0: |
| 115 | + return choice(PossibleMoves) |
| 116 | + else: |
| 117 | + return None |
| 118 | + |
| 119 | +#creating computers AI |
| 120 | +#this ai works on this algorithm |
| 121 | +#1.it checks if computer can win in next move,else |
| 122 | +#2.it checks if player can win in next move,then it blocks it,else |
| 123 | +#3.it chooses any available spaces from the corner[1,3,7,9],else |
| 124 | +#4.it fills middle square if space free,else |
| 125 | +#5.it chooses any available spaces from sides,ie,[2,4,6,8] |
| 126 | +#if you interchange the 3 and 4 steps the AI becomes little Hard,ie,it fills middle then corner |
| 127 | +#-------------------------------------- |
| 128 | +#Get computer move |
| 129 | +def GetComputerMove(board,ComputerLetter): |
| 130 | + if ComputerLetter == 'X': |
| 131 | + PlayerLetter = 'O' |
| 132 | + else: |
| 133 | + PlayerLetter = 'X' |
| 134 | + |
| 135 | + #1.check computer can win in next move |
| 136 | + for i in range(1,10): |
| 137 | + copy = GetBoardCopy(board) |
| 138 | + if IsSpaceFree(copy,i): |
| 139 | + MakeMove(copy,ComputerLetter,i) |
| 140 | + if IsWinner(copy,ComputerLetter): |
| 141 | + return i |
| 142 | + |
| 143 | + |
| 144 | + #2.check player can win in next move |
| 145 | + for i in range(1,10): |
| 146 | + copy = GetBoardCopy(board) |
| 147 | + if IsSpaceFree(copy,i): |
| 148 | + MakeMove(copy,PlayerLetter,i) |
| 149 | + if IsWinner(copy,PlayerLetter): |
| 150 | + return i |
| 151 | + |
| 152 | + #3.checking for corner |
| 153 | + move = ChooseRandomFromList(board,[1,3,7,9]) |
| 154 | + if move != None: |
| 155 | + return move |
| 156 | + |
| 157 | + #4.checking for the center |
| 158 | + if IsSpaceFree(board,5): |
| 159 | + return 5 |
| 160 | + |
| 161 | + #5.checking for sides |
| 162 | + return ChooseRandomFromList(board,[2,4,6,8]) |
| 163 | + |
| 164 | +#--------------------------------------- |
| 165 | + |
| 166 | +#checking board is full or not |
| 167 | +def IsBoardFull(board): |
| 168 | + for i in range(1,10): |
| 169 | + if IsSpaceFree(board,i): |
| 170 | + return False |
| 171 | + return True |
| 172 | + |
| 173 | +#fuction to print the final win or tie board |
| 174 | +#made this to separate usual board and winning or tie board |
| 175 | +def FinalBoard(board,NeedSleep=True): |
| 176 | + print(' |-------------|') |
| 177 | + DrawBoard(board,NeedSleep) |
| 178 | + print(' |-------------|') |
| 179 | + |
| 180 | + |
| 181 | +#LETS START THE GAME |
| 182 | +Welcome() |
| 183 | +while True: |
| 184 | + #intialising board |
| 185 | + TheBoard = [' '] * 10 |
| 186 | + PlayerLetter,ComputerLetter = InputPlayerLetter() |
| 187 | + turn = WhoFirst() |
| 188 | + print(f'The {turn} will go first') |
| 189 | + |
| 190 | + #gameloop |
| 191 | + Playing = True |
| 192 | + while Playing: |
| 193 | + |
| 194 | + if turn == 'player': |
| 195 | + print(f" Turn => {turn}") |
| 196 | + HintInput = ['Hint','hint','H','h'] |
| 197 | + #taking players input |
| 198 | + move = GetPlayerMove(TheBoard) |
| 199 | + #if player needs Hint |
| 200 | + while move in HintInput: |
| 201 | + #following code gives hint to the user |
| 202 | + #it runs player letter to computer AI |
| 203 | + HintBox = [] |
| 204 | + for i in TheBoard: |
| 205 | + HintBox.append(i) |
| 206 | + hint = GetComputerMove(HintBox,PlayerLetter) |
| 207 | + MakeMove(HintBox,PlayerLetter,hint) |
| 208 | + print(f'HINT : placing at {hint} is better') |
| 209 | + FinalBoard(HintBox,False) |
| 210 | + move = GetPlayerMove(TheBoard) |
| 211 | + |
| 212 | + MakeMove(TheBoard,PlayerLetter,move) |
| 213 | + |
| 214 | + |
| 215 | + if IsWinner(TheBoard,PlayerLetter): |
| 216 | + FinalBoard(TheBoard) |
| 217 | + print('❤You have WON the game❤') |
| 218 | + Playing = not Playing |
| 219 | + elif IsBoardFull(TheBoard): |
| 220 | + FinalBoard(TheBoard) |
| 221 | + print('The game is TIE\nIts good to PLAY untill you WIN❤') |
| 222 | + Playing = not Playing |
| 223 | + else : |
| 224 | + DrawBoard(TheBoard) |
| 225 | + turn = 'computer' |
| 226 | + |
| 227 | + else : |
| 228 | + #computer move |
| 229 | + print(f" Turn => {turn}") |
| 230 | + move = GetComputerMove(TheBoard,ComputerLetter) |
| 231 | + MakeMove(TheBoard,ComputerLetter,move) |
| 232 | + |
| 233 | + |
| 234 | + if IsWinner(TheBoard,ComputerLetter): |
| 235 | + FinalBoard(TheBoard) |
| 236 | + print('❤Try again bro you will win❤') |
| 237 | + Playing = not Playing |
| 238 | + elif IsBoardFull(TheBoard): |
| 239 | + FinalBoard(TheBoard) |
| 240 | + print('The game is TIE\nIts good to PLAY untill you WIN❤') |
| 241 | + Playing = not Playing |
| 242 | + else : |
| 243 | + DrawBoard(TheBoard) |
| 244 | + turn = 'player' |
| 245 | + |
| 246 | + if not PlayAgain(): |
| 247 | + print("***❤GIVE STAR ONLY IF YOU LIKE❤***") |
| 248 | + break |
| 249 | + |
0 commit comments