how to create Tic-Toc-Toi Terminal Game Using Python, With Computer Simple Artificial Intelligence | python games | python examples for beginners

Namaste Friends,





                            Today's tutorial is very unique because here I am going to show you how we can create a simple tic toc toi terminal game using python, with simple computer artificial intelligence.


friend, 

if you are a beginner. then, this tutorial is created only for you. 


let's talk about this game.

here, 

friends, 
                we will create a very simple game with help of basic knowledge of python programming. and the juice feature of this game is, it will provide us an Artificial Intelligent Player.  Yeah yeah, with AI player, we can play this games.

friends, now many of you thinking what is Artificial Intelligence.

Q 1. What Is Artificial Intelligence or AI?


Ans. An Artificial Intelligence is a computer program that can work, and in many cases think like a human and also respond like a human. means these programs can respond like a human, for example in our games this program will play like an opposite player. 

here are my python codes.

friends, for better understanding I am using lots of comments.

only for you. So,

Keep Reading & Pay Attention.

1. Tic Toc Toi Game Written In Python Programming Language?




  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Written By:
#  S.S.B
#  surajsinghbisht054@gmail.com
#  bitforestinfo.blogspot.com
#
# 
#
#
##################################################
######## Please Don't Remove Author Name #########
############### Thanks ###########################
##################################################
#
#
# Import Module
import random

print """
=====================================
+ Welcome To Tic-Toc-Toi Game  +
=====================================
    :-  Created By Bitforestinfo


You Just Need To Enter Number of place,
where you want your mark.

Numbers System Is given below,
 

 1 | 2 | 3
 ---------
 4 | 5 | 6
 ---------
 7 | 8 | 9

So, let's Start Our Game

"""

#
# list of fields
board = [" " for i in range(1,10)]

# Get Input From Player and process
def get_player_input(board):
 # Get input
 p=int(raw_input("[+] Your Turn! Enter Your Number : "))-1
 # Check Input Field
 if board[p]==" ": 
  board[p] = 'X'
  return True
 else:
  print "[+] Wrong Input! Hahahaha !"
  return

# Print Current Status of Board
def print_current_stuation(board):
 print """
 ==================================
 + This Is Present Status Of Game +
 ==================================

  {} | {} | {}
  ------------
  {} | {} | {}
  ------------
  {} | {} | {}

 """.format(*board)
 return

# Computer Player
def computer_ai(board):
 empty = []
 # Get List Of Empty Fields
 for n, p in enumerate(board):
  if p == " ":
   empty.append(n)
 # Check, if their is any empty field
 if empty:
  # choice random field
  p=random.choice(empty)
  print "[=] Computer AI Turn[=]"
  board[p] = 'O'
  return True
 # if their is no empty field
 else:
  print "[+] Nothing Left! Game Tie#"
  exit(0)
 return

# Check Games Status
def check_game(board):
 # Get list of empty field
 empty = [i for i in board if i==" "]
 if not empty:
  # If Empty field not found
  print "[+] --- Game Over --- [+]"
 # Situation For Winning This Game
 situation = [
 [0,4,8],
 [0,1,2],
 [3,4,5],
 [6,7,8],
 [0,3,6],
 [1,4,7],
 [2,5,8],
 [2,4,6],
 ]
 # Check Situations of game
 for a,b,c in situation:
  if board[a]!=" " and board[a]== board[b] and board[b]==board[c]:
   if board[a]=="O":
    print "[+] hehehe! Loser! \n[+] Computer AI Is Game Winner"
   else:
    print "[+] Good! Good! You Winned This Game"
   exit(0)
 return

# Main Function 
def main():
 # loop for players turn again and again
 while True:
  # loop for taking input from you
  while True:
   a = get_player_input(board)
   if a:
    # if your provide input is valid
    break
  # print current game situation
  print_current_stuation(board)
  # check Game situations
  check_game(board)
  # computer turn
  computer_ai(board)
  # check game situation
  print_current_stuation(board)
  # check game situation
  check_game(board)

# Main Trigger 
if __name__ == '__main__':
 main()


or if you want to download latest example.

Then Check Here My Github Repository.

This Tutorial End Here! :)

if you like my blog. then,

don't forget to subscribe,
comment and share.

 Thanks for reading 
have a nice day

Share this

Related Posts

Previous
Next Post »

1 comments:

Write comments
7 April 2017 at 11:17 delete

Is my blog helpful for you?😊

Reply
avatar