2016-11-14 57 views
0

好吧,基本上我是用僞代碼創建一個tic tac toe遊戲。這很糟糕,因爲我看不到輸出。反正,Tic Tac Toe遊戲僞代碼

/* *程序由:科裏 *程序:井字遊戲 * / 模塊的Main() 布爾玩家=真 常整數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)檢查關係

回答

0

相應的回答是:

  1. 有很多修改的代碼中的有關條件檢查和其他「案件」,而玩遊戲這可能導致許多工作要做。如果你更具體地問你的問題會更好。

  2. 就對角線檢查而言,請執行與checkCol和checkRow相同的操作,而不是檢查values[index][index],其中index值對於行和列都是相同的。

  3. 對於領帶檢查,您可以保留一個標誌變量,當沒有行,列和對角線匹配時,將返回false

更新:

  • 對於相對對角線,則必須檢查values[index][SIZE-index-1]
  • +0

    那麼我會如何初始化索引?我會把它設置爲0嗎?像對於索引= 0到SIZE - 1和大小是3? – Cory

    +0

    是的,索引將從0運行到SIZE - 1。 – AlphaQ