井字游戏伪代码



好了,基本上我要用伪代码创建一个井字游戏。这很糟糕,因为我看不到输出。无论如何,

/**程序由:科里*程序:井字游戏*/模块的Main ()布尔播放器= true常量整数ROWS = 3, COLS = 3声明字符串值(行)[关口]=""、""、"","", "", "","", "", ""

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)检查领带

相应回答:

  1. 关于条件检查和其他可能在玩游戏时导致的"情况",你的代码中有很多修改要做。

  2. 如果你的问题问得更具体一些会更好。
  3. 就对角线检查而言,执行与checkCol和checkRow相同的检查,而不是检查values[index][index],其中index值对于行和列都是相同的。

  4. 对于领带检查,您可以保留一个标志变量,当没有行,列和对角线匹配时将返回false

更新:

  • 对于相反的对角线,您必须检查values[index][SIZE-index-1]
  • 最新更新