【问题标题】:Print tic tac toe board fortran90打印井字棋板fortran90
【发布时间】: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


【解决方案1】:

您的代码似乎有点复杂。我会在这里和那里简化它。

首先,您可以使用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 ttt

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-12
    • 2020-10-20
    • 2011-06-13
    • 1970-01-01
    相关资源
    最近更新 更多