嘿,我一直在编码工作4周左右,现在我正在尝试做一个tic toc拖曳游戏。 到目前为止,我已经做到了这一点,我可以玩游戏并找到胜利者。当所有9个空格都已满时,我对如何确定绘图有点困惑。
我还需要在找到胜利者时停止游戏。换句话说,当找到胜利者时,不可能再放一块。
如果有人可以提供帮助,那就太棒了!
以下是我确定胜利者的不太友好的代码。
// check rows
if (GameBoard[0, 0] == 'X' && GameBoard[0, 1] == 'X' && GameBoard[0, 2] == 'X') { message = "Winner winner chicen dinner Player 1 You won! GG"; }
if (GameBoard[1, 0] == 'X' && GameBoard[1, 1] == 'X' && GameBoard[1, 2] == 'X') { message = "Winner winner chicen dinner Player 1 You won! GG"; }
if (GameBoard[2, 0] == 'X' && GameBoard[2, 1] == 'X' && GameBoard[2, 2] == 'X') { message = "Winner winner chicen dinner Player 1 You won! GG"; }
// check columns
if (GameBoard[0, 0] == 'X' && GameBoard[1, 0] == 'X' && GameBoard[2, 0] == 'X') { message = "Winner winner chicen dinner Player 1 You won! GG"; }
if (GameBoard[0, 1] == 'X' && GameBoard[1, 1] == 'X' && GameBoard[2, 1] == 'X') { message = "Winner winner chicen dinner Player 1 You won! GG"; }
if (GameBoard[0, 2] == 'X' && GameBoard[1, 2] == 'X' && GameBoard[2, 2] == 'X') { message = "Winner winner chicen dinner Player 1 You won! GG"; }
// check diags
if (GameBoard[0, 0] == 'X' && GameBoard[1, 1] == 'X' && GameBoard[2, 2] == 'X') { message = "Winner winner chicen dinner Player 1 You won! GG"; }
if (GameBoard[0, 2] == 'X' && GameBoard[1, 1] == 'X' && GameBoard[2, 0] == 'X') { message = "Winner winner chicen dinner Player 1 You won! GG"; }
// check rows
if (GameBoard[0, 0] == 'O' && GameBoard[0, 1] == 'O' && GameBoard[0, 2] == 'O') { message = "Winner winner chicen dinner Player 2 You won! GG"; }
if (GameBoard[1, 0] == 'O' && GameBoard[1, 1] == 'O' && GameBoard[1, 2] == 'O') { message = "Winner winner chicen dinner Player 2 You won! GG"; }
if (GameBoard[2, 0] == 'O' && GameBoard[2, 1] == 'O' && GameBoard[2, 2] == 'O') { message = "Winner winner chicen dinner Player 2 You won! GG"; }
// check columns
if (GameBoard[0, 0] == 'O' && GameBoard[1, 0] == 'O' && GameBoard[2, 0] == 'O') { message = "Winner winner chicen dinner Player 2 You won! GG"; }
if (GameBoard[0, 1] == 'O' && GameBoard[1, 1] == 'O' && GameBoard[2, 1] == 'O') { message = "Winner winner chicen dinner Player 2 You won! GG"; }
if (GameBoard[0, 2] == 'O' && GameBoard[1, 2] == 'O' && GameBoard[2, 2] == 'O') { message = "Winner winner chicen dinner Player 2 You won! GG"; }
// check diags
if (GameBoard[0, 0] == 'O' && GameBoard[1, 1] == 'O' && GameBoard[2, 2] == 'O') { message = "Winner winner chicen dinner Player 2 You won! GG"; }
if (GameBoard[0, 2] == 'O' && GameBoard[1, 1] == 'O' && GameBoard[2, 0] == 'O') { message = "Winner winner chicen dinner Player 2 You won! GG"; }
答案 0 :(得分:2)
到目前为止,你做得很好,你似乎缺少的是功能的使用。例如,您可以定义一个函数来检查单行:
import cv2
import sys
import logging as log
import datetime as dt
from time import sleep
cascPath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascPath)
log.basicConfig(filename='webcam.log',level=log.INFO)
video_capture = cv2.VideoCapture(0)
anterior = 0
while True:
if not video_capture.isOpened():
print('Unable to load camera.')
sleep(5)
pass
# Capture frame-by-frame
ret, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30)
)
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
if anterior != len(faces):
anterior = len(faces)
log.info("faces: "+str(len(faces))+" at "+str(dt.datetime.now()))
# Display the resulting frame
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Display the resulting frame
cv2.imshow('Video', frame)
# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()
然后我们可以创建一个包装函数:
bool SingleRowComplete(int rowNum, char player) {
return GameBoard[rowNum, 0] == player && GameBoard[rowNum, 1] == player && GameBoard[rowNum, 2] == player;
}
您可以为列和主对角线创建类似的功能。
一旦关闭,请检查然后封装:
bool AnyRowComplete(char player) {
return SingleRowComplete(0, player) || SingleRowComplete(1, player) || SingleRowComplete(2, player);
}
我想作为最后一点,我会说好工作,现在要学习功能:)。
答案 1 :(得分:0)
就个人而言,我建议您在需要检查阵列时使用循环。它将使您的代码更清晰,更易于阅读。
现在,对于你的第一个问题:当你的游戏开始时,用一些既不是' X'也不是' O' (空字符串也是一个选项)。编写一个函数,在每个回合之后迭代整个数组并查找该初始化值。如果找到,继续游戏。如果未找到,请更改布尔标志变量以指示游戏结束并以平局结束。
public bool isFull(char[] arr){
for (int i = 0; i < arr.GetLength(0); i++){
for (int j = 0; j < arr.GetLength(1); j++){
if (arr[i, j] == initial_value){
//If array in row i and column j is equal to the inital
//value, return true.
return true;
}
}
}
return false;
}
对于你的第二个问题,我会把把一块放入电路板的动作变成一个单独的功能,这将检查我之前提到的标志。如果该标志等于&#34; true&#34;,则不允许该动作发生。我建议以这种方式设置标志:
public void placeGamePiece(Player p){
if (gameEndFlag == false){
//Do the turn
}
else
Console.WriteLine("The game is over!"); //Or any other print
//function you are using.
}
public void startGame(){
public bool gameEndFlag = false;
//..........
gameEndFlag = !isFull(GameBoard);
}
我希望我以足够的方式回答你所有的问题。如果您需要对我在此处撰写的任何内容进行任何澄清,请与我们联系。如果你还没有学习循环和函数,我建议你现在就学习它们。