好吧,基本上我要用伪代码创建一个井字游戏。这很糟糕,因为我看不到输出。无论如何,
/* *节目制作:科里 *节目:Tic Tac Toe 游戏 * / 模块主() 布尔玩家 = true 常量整数 ROWS = 3,COLS = 3 声明字符串值[ROWS][COLS] = "", "", "", “”、“”、“”、 “”、“”、“”
Call Module getIntro()
Call Module showBoard(values[][], ROWS, COLS)
Call Module playGame(values[][], COLS, ROWS, player)
End Module
Module getIntro()
Display "Hello, this is a simple tic tac toe game."
Display "It will rotate turns between players one and two,"
Display "While 3,3 would be the bottom right."
Display "Player 1 is X and player 2 is O"
End Module
Module showBoard(String values[][], Integer rows, Integer cols)
Declare Integer row, col
For row = 0 to rows - 1
For col = 0 to cols - 1
Display values[rows][cols]
End For
End For
End Module
Module playGame(String values[][], Integer cols, Integer rows, Boolean player) //places moves, checks for already placed moves
Declare Integer row, col
player = true
For row = 0 to rows - 1
For col = 0 to cols -1
If (player == true)
Display "First player's turn, please input your move"
Input values[row][col]
if (checkMove(values[][], rows, cols) == false)
Display "Invalid move, please try again"
Display "First player's turn again, please input your move"
Input values[row][col]
End If
values[row][col] = "X"
showBoard(values[row][col], rows, cols)
checkWin()
player = false
Else If (player == false)
Display "Second player's turn, please input your move"
Input values[row][col]
if (checkMove(values[][], rows, cols) == false)
Display "Invalid move, please try again"
Display "Second player's turn again, please input your move"
Input values[row][col]
End If
values[row][col] = "O"
showBoard(values[row][col], rows, cols)
checkWin()
player = true
End For
End For
End Module
Function Boolean checkMove(String values[][], Integer cols, Integer rows)
Declare Integer row, col
For row = 0 to rows - 1
For col = 0 to cols - 1
if (values[row][col] != "*")
Display "Player has already placed a move there"
End If
End For
End For
return false
End Function
Module checkWin()
checkRow(values[][], 3)
checkCol(values[][], 3)
checkDiagonal(values[][], 3, 3)
End Module
Module checkRow(String values[][], Integer rows) //checks horizontal win
Declare Integer row, col
For row = 0 to rows - 1
if (values[row][0] == "X")
Display "Player one has won!"
//player1Win = true
Else if (values[row][0] == "O")
Display "Player two has won!"
//player1Win = false
End If
End For
End Module
Module checkCol(String values[][], Integer cols) //checks vertical win
Declare Integer row, col
For col = 0 to cols -1
if (values[0][col] == "X")
Display "Player one has won!"
//player1Win = true
Else if (values[0][col] == "O")
Display "Player two has won!"
//player1Win = false
End If
End For
End Module
Module checkDiagonal(String values[][], Integer cols, Integer rows, Boolean player1Win) //checks Diagonal win
所以1)请检查我的代码并告诉我我是否做错了什么或者是否有办法让它看起来更好 2)我需要帮助检查对角线胜利。 3)检查关系
相应地回答:
关于条件检查和玩游戏时可能导致的其他“情况”,您的代码需要进行大量修改。如果你的问题问得具体一点就更好了。
就对角线检查而言,与 checkCol 和 checkRow 中的操作相同,而是检查
values[index][index]
,其中行和列的 index
值相同。为了进行平局检查,您可以保留一个标志变量,当没有行、列和对角线匹配时,该变量将返回
false
。更新:
values[index][SIZE-index-1]
。