Fortran 2d arrays/connect 4 game

In summary, this conversation was about creating a code for a connect 4 game using modules and subroutines. The person was stuck on how to declare where the X's and O's would show up and how to determine when 4 in a row has been found. The expert provided a summary of the code and suggested adding pseudocode for diagonal checking to complete the game.
  • #1
eng909
1
0
Hi, i have an assignment to make a code for a connect 4 game,using module and subroutines, but as of now I am stuck, and do not now where to go. Any help would be great. here's what I have so far. I'm stuck on how to declare where the X's and O's will show up and how to determine when 4 in a row has been found

MODULE datamod
!7 COLUMNS
!6 ROWS
INTEGER, DIMENSION (6,7):: BOARD
INTEGER:: PLAYER
END MODULE datamod


PROGRAM C4
use datamod
INTEGER:: X,O
DO
WRITE(*,*) "Player 1's turn"
CALL drawboard(j,i)
WRITE(*,*) 'In which column would you like to place your piece (X)?'
READ (*,*) X
!Code on where to put X

WRITE(*,*) "Player 2's turn"
CALL DRAWBOARD(j,i)
WRITE(*,*) 'In which column would you like to place your piece (O)?'
READ (*,*) O
!code for placement of O's

END DO
END PROGRAM


Subroutine drawboard (j,i)
USE datamod
IMPLICIT NONE
integer::i, j
1 FORMAT (a,\)
Print *, ' 1 2 3 4 5 6 7'
Do i=6,1,-1
Do j=1,7
Print 1, ' | '
If (board(j,i)==0) Then
Print 1, ' '
ELSE IF (board(j,i)==1) Then
Print 1, 'X'
Else If (board(j,i)==2) Then
Print 1, 'O'
Else If (board(j,i)==3) Then
Print 1, '4'
End if
End do
Print *, '|'
Print "(1x, 29('-'))"
End do
End Subroutine drawboard

thanks for any help!
 
Physics news on Phys.org
  • #2
</code>you can modify the drawboard subroutine to do what you need. I have provided some pseudocode to help:<code>Subroutine drawboard (j,i) USE datamod IMPLICIT NONE integer::i, j 1 FORMAT (a,\) Print *, ' 1 2 3 4 5 6 7' Do i=6,1,-1 Do j=1,7 Print 1, ' | ' If (board(j,i)==0) Then Print 1, ' ' ELSE IF (board(j,i)==1) Then Print 1, 'X' Else If (board(j,i)==2) Then Print 1, 'O' Else If (board(j,i)==3) Then Print 1, '4' End if End do Print *, '|' Print "(1x, 29('-'))" End do!Check for 4 in a rowDo i=1,7 Do j=1,6 If (board(j,i)==player &amp;&amp; board(j+1,i)==player &amp;&amp; board(j+2,i)==player &amp;&amp; board(j+3,i)==player) Then Print *, "Player", player, "wins!" End Program End If End doEnd doDo i=1,6 Do j=1,7 If (board(j,i)==player &amp;&amp; board(j,i+1)==player &amp;&amp; board(j,i+2)==player &amp;&amp; board(j,i+3)==player) Then Print *, "Player", player, "wins!" End Program End If End doEnd doEnd Subroutine drawboard</code>This should check for a 4 in a row horizontally and vertically. You can also add diagonal checking.
 

Related to Fortran 2d arrays/connect 4 game

1. What is a 2D array in Fortran?

A 2D array in Fortran is a data structure that stores data in a grid-like format with rows and columns. It allows for efficient storage and manipulation of large sets of data.

2. How are 2D arrays created in Fortran?

To create a 2D array in Fortran, you need to declare the array with its dimensions and data type, and then use a DO loop to assign values to each element in the array.

3. How can 2D arrays be used in a Connect 4 game?

In a Connect 4 game, a 2D array can be used to represent the game board. Each element in the array can store a value representing a player's piece, making it easy to check for winning combinations and keep track of the game's progress.

4. Can 2D arrays in Fortran be dynamically resized?

Yes, 2D arrays in Fortran can be dynamically resized using the ALLOCATE statement. This allows for more flexibility in handling different data sizes and can be useful in situations where the size of the game board may change.

5. How do you access and modify elements in a 2D array in Fortran?

To access and modify elements in a 2D array in Fortran, you can use the array index notation, specifying the row and column of the element you want to access or modify. For example, to access the element in the second row and third column, you would use array(2,3).

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
868
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
Back
Top