Python implements simple tic-tac-toe game

Tic-tac-toe, the English name is Tic-Tac-Toe, is a kind of renju game played on a 3*3 grid. It is similar to Gobang. Because the board generally does not have a border, the grid lines are arranged in tic-tac-toe so it is named. The only tools needed for the game are paper and pen, and then two players representing O and X take turns to leave marks in the grid (generally, the first player is X), any three marks forming a straight line, it is a win .

The difficulty of the game is how to judge that the connection is a line; horizontal, vertical and oblique directions;

Game code:

#! /usr/bin/env python3
# - *- coding:utf-8-*-
u'''
Created on April 13, 2019

@ author: wuluo
'''
__ author__ ='wuluo'
__ version__ ='1.0.0'
__ company__ = u'Chongqing Jiaotong University'
__ updated__ ='2019-04-13'

# Program for creating tic-tac-toe
def initBoard():
 global board #Call the global board
 board =[None]*3print("Tic-Tac-Toe:")for i inrange(len(board)):
 board[i]=["+ "]*3

# Program for printing tic-tac-toe
def printBoard():
 global board
 for i inrange(len(board)):for j inrange(len(board[i])):print(board[i][j], end=" ")print("")

# The program to start playing chess
def startGame():
 global board
 player =0whileisGameContinue():if player <=8:if player %2==0:
 # Party A plays chess
 print("==Black plays chess")if not playChess("x"):continueelse:
 # Party B plays chess
 print("==White plays chess")if not playChess("○"):continue
 player +=1else:print("draw")break

def playChess(chess):
 # Get location
 x =int(input("==  X="))-1
 y =int(input("==  Y="))-1if board[x][y]=="+ ":
 board[x][y]= chess
 printBoard()return True #Successful
 else:print("==There are already pieces, please re-set\a")printBoard()return False #Failed

def isGameContinue():for i inrange(len(board)):for j inrange(len(board[i])):if board[i][j]!="+ ":
 # Horizontal
 if j ==0:if board[i][j]== board[i][j +1]== board[i][j +2]:whoWin(i, j)return False
 # Vertical
 if i ==0:if board[i][j]== board[i +1][j]== board[i +2][j]:whoWin(i, j)return False
 # Forward oblique
 if i ==0 and j ==0:if board[i][j]== board[i +1][j +1]== board[i +2][j +2]:whoWin(i, j)return False
 # Backslash
 if i ==2 and j ==0:if board[i][j]== board[i -1][j +1]== board[i -2][j +2]:whoWin(i, j)return False
 return True

def whoWin(i, j):if board[i][j]=="x":print("Black wins!")else:print("White wins!")for i inrange(3):print("win")classmain():
 board =[]initBoard()printBoard()startGame()if __name__ =="__main__":main()

Game results:

Another result is a tie:

The above is the whole content of this article, I hope it will be helpful to everyone's study.

Recommended Posts

Python implements simple tic-tac-toe game
Python implements tic-tac-toe game
Python implements tic-tac-toe game
Python implements Tetris game
Python implements minesweeper game
Python implements guessing game
Python implements WeChat airplane game
Python implements word guessing game
Python implements a guessing game
Python implements digital bomb game
Python implements the brick-and-mortar game
Python simply implements the snake game
Python implements digital bomb game program
Python implements the source code of the snake game
Python implements a simple business card management system
Python implements Super Mario
Python implements man-machine gobang
Python, PyGame game project
Python implements image stitching
Python implements scanning tools
Python implements threshold regression
Python implements minesweeper games
Python implements electronic dictionary
Python3 realizes airplane war game
python guess the word game
Python implements parking management system
Python realizes apple eating game
Python implements TCP file transfer
Python realizes the guessing game
Python parses simple XML data
Python numpy implements rolling case
OpenCV Python implements puzzle games
Python implements password strength verification
Python implements car management system
Python implements code block folding
Python implements panoramic image stitching
Python implements SMTP mail sending
Python implements multi-dimensional array sorting
How Python implements FTP function
Python implements mean-shift clustering algorithm
Python implements verification code recognition
Python implements gradient descent method
Python implements text version minesweeper
Python implements image stitching function
Python implements student performance evaluation system
Python3 implements the singleton design pattern
Python implements exchange rate conversion operations
Python implements ten classic sorting algorithms
Python implements a universal web framework
Python implements 126 mailboxes to send mail
Python implements AI face change function
Python implements the steepest descent method
Python implements the actual banking system
Simple usage of python definition class
Python decorator simple usage example summary
Python implements ftp file transfer function
Python implements username and password verification
How Python implements the timer function
Python implements the aircraft war project
Python implements horizontal stitching of pictures
Python implements GIF graph upside down