做 loop/if 语句逻辑
do loop/if statement logic
我正在开发一个井字游戏程序。函数 CHECK_WINNER 应该接收给定状态下的棋盘,并确定游戏是否获胜、平局,或者玩家是否需要继续游戏。 CHECK_WINNER 根据棋盘的状态取不同的值。
win(n;1:3) 块列出了井字棋盘的 8 种可能获胜配置。
我认为问题出在 do 循环中。我的目标是为每个玩家(1 和 2)循环遍历棋盘的每个获胜场景,并检查是否满足获胜条件。 CHECK_MOVE 如果玩家 1 获胜则应为 1,如果玩家 2 获胜则应为 2,如果游戏平局则应为 3,对于任何其他情况应为 0。我究竟做错了什么?
program checkwinner
implicit none
integer, dimension(9) :: board
integer, external :: CHECK_WINNER
integer :: cw
!board = (/ 1,2,1,2,1,2,2,2,1 /) ! not working correctly. 1 is winner, board full; cw returns 3
!board = (/ 1,2,1,2,0,2,2,2,1 /) ! working correctly. no winner, open spaces; cw returns 0
!board = (/ 1,1,1,0,0,0,0,0,0 /) ! not working. cw should return 1; instead cw returns 0
board = (/ 2,2,2,0,0,0,0,0,0 /) ! not working
cw = CHECK_WINNER(board)
print *, board(1:3)
print *, board(4:6)
print *, board(7:9)
print *, cw
end program checkwinner
!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!
integer function CHECK_WINNER(fboard)
implicit none
integer :: i
integer, dimension(8,3) :: win
integer, dimension(9), intent(in) :: fboard
win(1,1:3) = (/ 1,2,3 /)
win(2,1:3) = (/ 4,5,6 /)
win(3,1:3) = (/ 7,8,9 /)
win(4,1:3) = (/ 1,4,7 /)
win(5,1:3) = (/ 2,5,8 /)
win(6,1:3) = (/ 3,6,9 /)
win(7,1:3) = (/ 1,5,9 /)
win(8,1:3) = (/ 3,5,7 /)
do i = 1,8
if (fboard(win(i,1)) == 1 .and. fboard(win(i,2)) == 1 .and. fboard(win(i,3)) == 1) then
CHECK_WINNER = 1
else if (fboard(win(i,1)) == 2 .and. fboard(win(i,2)) == 2 .and. fboard(win(i,3)) == 2) then
CHECK_WINNER = 2
else if ((fboard(win(i,1)) == 1 .or. fboard(win(i,1)) == 2) .and. (fboard(win(i,2)) == 1 .or. &
fboard(win(i,2)) == 2) .and. (fboard(win(i,3)) == 1 .or. fboard(win(i,3)) == 2)) then
CHECK_WINNER = 3
else
CHECK_WINNER = 0
end if
end do
end function CHECK_WINNER
因此,在您在代码中使用 board = (/ 2,2,2,0,0,0,0,0,0 /)
给出的示例中,当函数的 do 循环中 i=1
时,它正确设置了 CHECK_WINNER=2 因为
fboard(win(i,1)) == 2 .and. fboard(win(i,2)) == 2 .and. fboard(win(i,3)) == 2
是真的。不幸的是,当循环到达 i=2、i=3 等时,它会将 CHECK_WINNER 重置为零,因为获胜条件的 none 匹配。这是导致问题的 else
块。将 do 循环更改为
CHECK_WINNER = 0
do i = 1,8
if (fboard(win(i,1)) == 1 .and. fboard(win(i,2)) == 1 .and. fboard(win(i,3)) == 1) then
CHECK_WINNER = 1
else if (fboard(win(i,1)) == 2 .and. fboard(win(i,2)) == 2 .and. fboard(win(i,3)) == 2) then
CHECK_WINNER = 2
else if ((fboard(win(i,1)) == 1 .or. fboard(win(i,1)) == 2) .and. (fboard(win(i,2)) == 1 .or. &
fboard(win(i,2)) == 2) .and. (fboard(win(i,3)) == 1 .or. fboard(win(i,3)) == 2)) then
CHECK_WINNER = 3
end if
end do
因此,如果它找到已知组合,它会设置 CHECK_WINNER。您也可以在每个 (else)if 子句中使用 exit
找到已知组合后立即退出循环。 (设置CHECK_WINNER后)
我正在开发一个井字游戏程序。函数 CHECK_WINNER 应该接收给定状态下的棋盘,并确定游戏是否获胜、平局,或者玩家是否需要继续游戏。 CHECK_WINNER 根据棋盘的状态取不同的值。
win(n;1:3) 块列出了井字棋盘的 8 种可能获胜配置。
我认为问题出在 do 循环中。我的目标是为每个玩家(1 和 2)循环遍历棋盘的每个获胜场景,并检查是否满足获胜条件。 CHECK_MOVE 如果玩家 1 获胜则应为 1,如果玩家 2 获胜则应为 2,如果游戏平局则应为 3,对于任何其他情况应为 0。我究竟做错了什么?
program checkwinner
implicit none
integer, dimension(9) :: board
integer, external :: CHECK_WINNER
integer :: cw
!board = (/ 1,2,1,2,1,2,2,2,1 /) ! not working correctly. 1 is winner, board full; cw returns 3
!board = (/ 1,2,1,2,0,2,2,2,1 /) ! working correctly. no winner, open spaces; cw returns 0
!board = (/ 1,1,1,0,0,0,0,0,0 /) ! not working. cw should return 1; instead cw returns 0
board = (/ 2,2,2,0,0,0,0,0,0 /) ! not working
cw = CHECK_WINNER(board)
print *, board(1:3)
print *, board(4:6)
print *, board(7:9)
print *, cw
end program checkwinner
!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!
integer function CHECK_WINNER(fboard)
implicit none
integer :: i
integer, dimension(8,3) :: win
integer, dimension(9), intent(in) :: fboard
win(1,1:3) = (/ 1,2,3 /)
win(2,1:3) = (/ 4,5,6 /)
win(3,1:3) = (/ 7,8,9 /)
win(4,1:3) = (/ 1,4,7 /)
win(5,1:3) = (/ 2,5,8 /)
win(6,1:3) = (/ 3,6,9 /)
win(7,1:3) = (/ 1,5,9 /)
win(8,1:3) = (/ 3,5,7 /)
do i = 1,8
if (fboard(win(i,1)) == 1 .and. fboard(win(i,2)) == 1 .and. fboard(win(i,3)) == 1) then
CHECK_WINNER = 1
else if (fboard(win(i,1)) == 2 .and. fboard(win(i,2)) == 2 .and. fboard(win(i,3)) == 2) then
CHECK_WINNER = 2
else if ((fboard(win(i,1)) == 1 .or. fboard(win(i,1)) == 2) .and. (fboard(win(i,2)) == 1 .or. &
fboard(win(i,2)) == 2) .and. (fboard(win(i,3)) == 1 .or. fboard(win(i,3)) == 2)) then
CHECK_WINNER = 3
else
CHECK_WINNER = 0
end if
end do
end function CHECK_WINNER
因此,在您在代码中使用 board = (/ 2,2,2,0,0,0,0,0,0 /)
给出的示例中,当函数的 do 循环中 i=1
时,它正确设置了 CHECK_WINNER=2 因为
fboard(win(i,1)) == 2 .and. fboard(win(i,2)) == 2 .and. fboard(win(i,3)) == 2
是真的。不幸的是,当循环到达 i=2、i=3 等时,它会将 CHECK_WINNER 重置为零,因为获胜条件的 none 匹配。这是导致问题的 else
块。将 do 循环更改为
CHECK_WINNER = 0
do i = 1,8
if (fboard(win(i,1)) == 1 .and. fboard(win(i,2)) == 1 .and. fboard(win(i,3)) == 1) then
CHECK_WINNER = 1
else if (fboard(win(i,1)) == 2 .and. fboard(win(i,2)) == 2 .and. fboard(win(i,3)) == 2) then
CHECK_WINNER = 2
else if ((fboard(win(i,1)) == 1 .or. fboard(win(i,1)) == 2) .and. (fboard(win(i,2)) == 1 .or. &
fboard(win(i,2)) == 2) .and. (fboard(win(i,3)) == 1 .or. fboard(win(i,3)) == 2)) then
CHECK_WINNER = 3
end if
end do
因此,如果它找到已知组合,它会设置 CHECK_WINNER。您也可以在每个 (else)if 子句中使用 exit
找到已知组合后立即退出循环。 (设置CHECK_WINNER后)