- Related Questions & Answers
- Add minimum number to an array so that the sum becomes even in C programming
- C/C++ Program for nth Catalan Number?
- C Program to Check if all digits of a number divide it
- Concatenate a string given number of times in C++ programming
- Superperfect Number in C programming
- HTML DOM Input Number value Property
- HTML DOM Input Number type Property
- How to find the number of days in a month of a particular year in Java?
- Number of elements a particular tag contains in JavaScript?
- Print multiples of Unit Digit of Given Number in C Program
- C/C++ Programming to Count trailing zeroes in factorial of a number?
- C++ Programming for Smallest K digit number divisible by X?
- To find sum of even factors of a number in C++ Program?
- 10 Interesting Python Cool Tricks
- howdoi in Python

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

This is very funny game. In this game no player is needed, it is an automatic game.

Here we are using two Python modules numpy and random. In this game the mark on the board is put automatically instead of asking the user to put a mark on the board, and it will display the board after each turn unless a player wins. It returns -1 If the game gets draw.

import numpy as np import random from time import sleep # first creates an empty board def my_create_board(): return(np.array([[0, 0, 0], [0, 0, 0], [0, 0, 0]])) # Check for empty places on board def my_possibilities(board): l = [] for i in range(len(board)): for j in range(len(board)): if board[i][j] == 0: l.append((i, j)) return(l) # Select a random place for the player def my_random_place(board, my_player): selection = my_possibilities(board) current_loc = random.choice(selection) board[current_loc] = my_player return(board) def my_row_win(board, my_player): for x in range(len(board)): win = True for y in range(len(board)): if board[x, y] != my_player: win = False continue if win == True: return(win) return(win) def my_col_win(board, my_player): for x in range(len(board)): win = True for y in range(len(board)): if board[y][x] != my_player: win = False continue if win == True: return(win) return(win) def my_diag_win(board, my_player): win = True: for x in range(len(board)): if board[x, x] != my_player: win = False return(win) def evaluate_game(board): my_winner = 0 for my_player in [1, 2]: if (my_row_win(board, my_player) or my_col_win(board,my_player) or my_diag_win(board,my_player)): my_winner = my_player if np.all(board != 0) and my_winner == 0: my_winner = -1 return my_winner # Main function to start the game def my_play_game(): board, my_winner, counter = my_create_board(), 0, 1 print(board) sleep(2) while my_winner == 0: for my_player in [1, 2]: board = my_random_place(board, my_player) print("Board after " + str(counter) + " move") print(board) sleep(2) counter += 1 my_winner = evaluate_game(board) if my_winner != 0: break return(my_winner) # Driver Code print("Winner is: " + str(my_play_game()))

[[0 0 0] [0 0 0] [0 0 0]] Board after 1 move [[0 1 0] [0 0 0] [0 0 0]] Board after 2 move [[0 1 0] [0 0 0] [0 0 2]] Board after 3 move [[0 1 0] [0 0 0] [0 1 2]] Board after 4 move [[0 1 0] [0 0 2] [0 1 2]] Board after 5 move [[0 1 1] [0 0 2] [0 1 2]] Board after 6 move [[0 1 1] [2 0 2] [0 1 2]] Board after 7 move [[0 1 1] [2 1 2] [0 1 2]] Winner is: 1

Advertisements