我只是想创建一个子程序,在tic tac toe中打印出电路板的当前状态。0是空格,1是X,2是O。我不知道我的代码出了什么问题,但我会打印出当前的状态,而不是打印出一堆不同的、不正确的电路板。任何帮助都将不胜感激。
!这是我的代码和子例程:
program ttt
implicit none
integer, dimension(9) :: board
integer :: playerx
character (len=1), dimension(9) :: cboard
integer :: i, j
!Print board with numbered spots
!print *, "Enter a number 1-9 to play tic-tac-toe"
print *, " "
print "(a11)", " 1 | 2 | 3 "
print "(a11)", "---+---+---"
print "(a11)", " 4 | 5 | 6 "
print "(a11)", "---+---+---"
print "(a11)", " 7 | 8 | 9 "
print *, " "
board = (/ 2, 0, 0, &
0, 1, 0, &
0, 0, 1 /)
playerx = 1
call printboard(board, playerx)
end program ttt
! Subroutine to print out the current state of the board
subroutine printboard(board, playerx)
implicit none
integer, intent(in), dimension(9) :: board
integer, intent(in) :: playerx
character (len=1), dimension(9) :: cboard
integer :: i, j
! board array is series of 1s, 2s, and 0s. set 1 = x, 2 = o, and 0 = " "
if (playerx == 1) then
do i = 1,9
do j = 1, 9
if (board(i) == 0) cboard(j) = " "
if (board(i) == 1) cboard(j) = "x"
if (board(i) == 2) cboard(j) = "o"
if (j < 0 .and. j < 4) then
print "(a1, a1, a3, a1, a3, a1)", " ", cboard(j), " | ", cboard(j), " | ", cboard (j)
print "(a11)", "---+---+---"
endif
if (j > 3 .and. j < 7) then
print "(a1, a1, a3, a1, a3, a1)", " ", cboard(j), " | ", cboard(j), " | ", cboard (j)
print "(a11)", "---+---+---"
endif
if (j > 6 .and. j < 10) then
print "(a1, a1, a3, a1, a3, a1)", " ", cboard(j), " | ", cboard(j), " | ", cboard (j)
print "(a11)", " "
endif
endif
end subroutine printboard这是代码产生的结果:
1 | 2 | 3
---+---+---
4 | 5 | 6
---+---+---
7 | 8 | 9
o | o | o
---+---+---
o | o | o
---+---+---
o | o | o
---+---+---
o | o | o
o | o | o
o | o | o
| |
---+---+---
| |
---+---+---
| |
---+---+---
| |
| |
| |
| |
---+---+---
| |
---+---+---
| |
---+---+---
| |
| |
| |
| |
---+---+---
| |
---+---+---
| |
---+---+---
| |
| |
| |
x | x | x
---+---+---
x | x | x
---+---+---
x | x | x
---+---+---
x | x | x
x | x | x
x | x | x
| |
---+---+---
| |
---+---+---
| |
---+---+---
| |
| |
| |
| |
---+---+---
| |
---+---+---
| |
---+---+---
| |
| |
| |
| |
---+---+---
| |
---+---+---
| |
---+---+---
| |
| |
| |
x | x | x
---+---+---
x | x | x
---+---+---
x | x | x
---+---+---
x | x | x
x | x | x
x | x | x发布于 2020-11-29 07:55:15
你的代码看起来有点复杂。我会在这里和那里简化它。
首先,您可以使用2D数组来存储数据。我觉得这样会更自然。
不需要根据来自integer, dimension(3,3) :: board的值填充character(1), dimension(3,3) :: cboard,您可以简单地引入函数,该函数将根据存储在board中的值提供所需的character。
当涉及到打印时,我认为放置多个行为不同的print部分(取决于是否会有一个新行)是一种过度的杀伤力。
将部分代码(负责应用程序的特定逻辑)放入模块中也是一个好主意。这样,您就可以重用和替换它们。例如,你可以很容易地添加新的模块,以不同的方式打印你的电路板。
当涉及到Fortran时,最好记住数组中的列/行顺序。对于来自不同语言的人来说,这可能会让人感到困惑。
module board_printer
implicit none
contains
function integer2char(x)
integer, intent(in) :: x
character(1) :: integer2char
if(x == 0) then integer2char = ' '
else if(x == 1) then integer2char = 'x'
else integer2char = 'o'
end if
end function integer2char
subroutine printboard(board)
integer, intent(in), dimension(3, 3) :: board
integer :: i
do i = 1, 3
write(*, fmt="(a)") ' '//integer2char(board(i,1))//' | '//integer2char(board(i,2))//' | '//integer2char(board(i,3))//' '
if( i /= 3) then
write(*, fmt="(a)") '---+---+---'
end if
end do
write(*,*) ""
end subroutine printboard
end module board_printer
program ttt
use board_printer
implicit none
integer, dimension(3, 3) :: board
write(*,fmt="(a)") "Enter a number 1-9 to play tic-tac-toe"
write(*,fmt="(a)") " "
write(*,fmt="(a)") ' 1 | 4 | 7 '
write(*,fmt="(a)") '---+---+---'
write(*,fmt="(a)") ' 2 | 5 | 8 '
write(*,fmt="(a)") '---+---+---'
write(*,fmt="(a)") ' 3 | 6 | 9 '
write(*,fmt="(a)") ''
board = reshape((/ 0, 0, 0, 0, 0, 0, 0, 0, 0 /), shape(board) )
call printboard(board)
board = reshape((/ 1, 0, 0, 0, 1, 0, 0, 0, 1 /), shape(board) )
call printboard(board)
board = reshape((/ 0, 0, 1, 0, 1, 0, 1, 0, 0 /), shape(board) )
call printboard(board)
board = reshape((/ 2, 0, 0, 0, 2, 0, 0, 0, 2 /), shape(board) )
call printboard(board)
board = reshape((/ 1, 0, 2, 0, 1, 0, 2, 0, 1 /), shape(board) )
call printboard(board)
end program ttthttps://stackoverflow.com/questions/65028241
复制相似问题