current position:Home>How to set a draw when making tic tac toe chess in Python
How to set a draw when making tic tac toe chess in Python
2022-02-02 18:14:58 【CSDN Q & A】
Would like to ask this code how to set a draw
info = [['.', '.', '.'], ['.', '.', '.'], ['.', '.', '.']]def show(): print(' 0 1 2') for i in range(3): print(i, info[i][0], info[i][1], info[i][2])def judge(): if info[0][0] == info[1][1] and info[1][1] == info[2][2]: if info[0][0] != '.': return info[0][0] if info[0][2] == info[1][1] and info[1][1] == info[2][0]: if info[0][2] != '.': return info[0][2] for i in range(3): if info[i][0] == info[i][1] and info[i][1] == info[i][2]: if info[i][0] != '.': return info[i][0] for i in range(3): if info[0][i] == info[1][i] and info[1][i] == info[2][i]: if info[0][i] != '.': return info[0][i] return 'y'show()while True: a = input(' The player x, Which line do you want to put the pieces on :') b = input(' The player x, Which row do you want to put the pieces in :') a = int(a) b = int(b) info[a][b] = 'x' show() flag = judge() if flag == 'x': print(' The player x victory ') break; a = input(' The player o, Which line do you want to put the pieces on :') b = input(' The player o, Which row do you want to put the pieces in :') a = int(a) b = int(b) info[a][b] = 'o' show() flag = judge() if flag == 'o': print(' The player o victory ') break;
Refer to the answer 1:
Your code doesn't take into account :1、 User input error ;2、 There are already pieces in the chess position . I added it all , You can have a look , Leave a message if you have any questions
info = [['.', '.', '.'], ['.', '.', '.'], ['.', '.', '.']]def show(): print(' 0 1 2') for i in range(3): print(i, info[i][0], info[i][1], info[i][2])def judge(): if info[0][0] == info[1][1] and info[1][1] == info[2][2]: if info[0][0] != '.': return info[0][0] if info[0][2] == info[1][1] and info[1][1] == info[2][0]: if info[0][2] != '.': return info[0][2] for i in range(3): if info[i][0] == info[i][1] and info[i][1] == info[i][2]: if info[i][0] != '.': return info[i][0] for i in range(3): if info[0][i] == info[1][i] and info[1][i] == info[2][i]: if info[0][i] != '.': return info[0][i] l = [] for i in [0, 1, 2]: for j in [0, 1, 2]: l.append(info[i][j]) if '.' not in l: return 'z'show()while True: while True: while True: a = int(input(' The player x, Which line do you want to put the pieces on :')) if a not in [0, 1, 2]: print(' Incorrect input , Please re-enter ') else: break while True: b = int(input(' The player x, Which row do you want to put the pieces in :')) if a not in [0, 1, 2]: print(' Incorrect input , Please re-enter ') else: break if info[a][b] == '.': info[a][b] = 'x' break else: print(' There are pieces in this position ') show() flag = judge() if flag == 'x': print(' The player x victory ') break if flag == 'z': print(' It ends in a draw ') break while True: while True: a = int(input(' The player o, Which line do you want to put the pieces on :')) if a not in [0, 1, 2]: print(' Incorrect input , Please re-enter ') else: break while True: b = int(input(' The player o, Which row do you want to put the pieces in :')) if a not in [0, 1, 2]: print(' Incorrect input , Please re-enter ') else: break if info[a][b] == '.': info[a][b] = 'o' break else: print(' There are pieces in this position ') show() flag = judge() if flag == 'o': print(' The player o victory ') break if flag == 'z': print(' It ends in a draw ') break
Refer to the answer 2:
copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202021814558306.html
The sidebar is recommended
- Design a mathutils Java class file
- Design of identity authentication based on Lamport scheme
- How does Simulink simulate the neutral point of permanent magnet synchronous motor (connected to the midpoint of bus capacitance)?
- Excuse me: it is known that 9 of the 300 experimental samples have been wrongly divided. What is the penalty factor C in the SVM model?
- Is there any common Python tutorial that even a fool can understand?
- The pointer of the derived class shows why the value of the pointer data member converted to the base class has changed. Please explain
- Initialization of structure variables, application
- How can the data in the red box be displayed in the black box classification according to the status?
- Position in CSS: after fixed, it will appear in the original position after entering the page, and then flash to the fixed position
- How do the from attribute and to attribute of QML propertyanimation change
guess what you like
-
Want to know what this code means
-
Design a mathutils Java class file
-
KNN algorithm can not come up with the best model selection
-
After deployment, the project automatically reads the files under the login machine disk
-
C # click the button to display the name of the folder on the control
-
visual studio
-
The use of cleanstate
-
I don't understand what this means!!
-
Machine learning Jupiter notebook spam filtering analysis report
-
Run result and error content Print (dict_data ['content '] ['out']) keyerror: 'content, how to solve it?
Random recommended
- Appium code failed to open mobile app
- About the problems of C language 2022 calendar event recording system! (development tool eclipse)
- Restart Ubuntu as soon as VMware starts
- The computer network is normal, but some web pages can't be opened. What should I do
- Why can't you query after adding information
- The reinstallation system has been stuck in "starting windows"
- How did my visual studio code change like this
- Alien project spacecraft collision error
- Opencv play nine palace game
- What library does spider need to add? Why doesn't Anaconda
- Why not exchange? C language pointer
- There is a problem with my SQL statement
- First establish the input data into a binary sort tree, and then find the data. Enter the keyword sequence (63, 90, 70, 55, 67, 42, 98, 83, 10, 45, 58). 1. Establish a binary sort tree according to the order of the entered keywords.
- Why is x output 13 instead of 3
- Python code, interface problems
- Python basic problems, read and write files, and solve them in a basic way
- CSDN article reading statistics algorithm logic
- Swift5 dynamically obtain the height of the navigation bar and status bar
- A small problem about string []
- Why is it not displayed in graphicsview
- MATLAB image hiding problem, please help
- Python fastobjectlistview cannot operate on setitem * *
- What should I fill in at the crosswalk
- College C language programming c + + Programming
- Appium + Python + Android JDK failed to connect to the mobile phone to run app
- I think the activity can be started in the background above Android Q. how to realize it
- Use the predict type parameter to define a method for filtering books
- Problems related to the use of C PropertyGrid
- How to set the gain & lt^ u
- Click one H5 item in the app to jump to another H5 item
- Find a solution to the error of this r code
- Python will test Put the PY file in a python package (such as D: \ TT), import the package test in test2, and output the value of the variable data.
- When using PPO strategy to train reinforcement learning model, how to evaluate the quality of the model and judge whether it converges?
- Calculate the number of uppercase letters, lowercase letters and numbers in any string
- If you work from home, the same operation will fail
- Content encapsulation under three-level classification of Java
- The idea switches branches and the project directory structure disappears
- 1. Customize a - function to realize the following functions: input the accuracy e, and use the following formula to calculate the approximate value of cosx until the absolute value of the last - item is less than E. It is required to define and call the
- Find its Huffman code? Give the coding process and coding results (please give the large probability as 1 and the small probability as 0) Calculate the average code length. Calculate the compression ratio before and after coding
- C language and documents to solve students' performance problems