【发布时间】:2021-03-09 16:47:17
【问题描述】:
我只是想创建一个子程序来打印井字棋棋盘的当前状态。零是空格,1 是 X,2 是 O。我不确定我的代码有什么问题,但 ti 会打印出当前状态,而是打印出一堆不同的、不正确的板。任何帮助都将不胜感激。
!!!!!!!!!这是我的代码和子程序:
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
【问题讨论】:
-
欢迎您,请拨打tour。什么是预期的输出?当前输出有什么问题?
-
仔细考虑在
j的循环中发生了什么以及它与外部i循环的交互。 -
我想帮忙,但它看起来像一个家庭作业问题......什么是打印,(“全部”)?
-
@Holmz,您的字体是否没有将编辑描述符显示为
a11而不是all? -
谢谢@francescalus,昨天公交车上是iPad,所以可能是车太有弹性了。
标签: arrays fortran fortran90 tic-tac-toe