Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 10:35 26 Nov 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Loading data into a multidimension array

Author Message
PEnthymeme

Regular Member

Joined: 27/12/2023
Location: United Kingdom
Posts: 42
Posted: 04:12pm 25 Feb 2024
Copy link to clipboard 
Print this post

I am looking to load values into an 8,3 array...

And my initial attempt is below (which I know doesn't work ;-) )

DIM WIN_PATTERNS(8, 3)
WIN_PATTERNS = ((1, 2, 3), (4, 5, 6), (7, 8, 9), (1, 4, 7), (2, 5, 8), (3, 6, 9), (1, 5, 9),(3, 5, 7))
 


I could break the WIN_PATTERNS into a data statement and read into the array for example:

DATA 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 4, 7, 2, 5, 8, 3, 6, 9, 1, 5, 9,3, 5, 7


and iterate over the arrays.

No problems there.

But, is there a way of loading this into the array directly?

Cheers
P
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3150
Posted: 04:26pm 25 Feb 2024
Copy link to clipboard 
Print this post

Try
DIM WIN_PATTERNS(7,2)= (1,2,3,4,5,6,7,8,9,1,4,7,2,5,8,3,6,9,1,5,9,3,5,7)
for i=0 to 2:for j=0 to 7:?WIN_PATTERNS(j,i):next j:next i

PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2129
Posted: 04:37pm 25 Feb 2024
Copy link to clipboard 
Print this post

DIM WIN_PATTERNS(8, 3),a,b,v
restore
for a=0 to 7
 for b=0 to 2
   read v:WIN_PATTERNS(a, b)=v
 next b
next a

for a=0 to 7
 for b=0 to 2
   print WIN_PATTERNS(a, b)
 next b
next a

end

DATA 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 4, 7, 2, 5, 8, 3, 6, 9, 1, 5, 9,3, 5, 7
 
PEnthymeme

Regular Member

Joined: 27/12/2023
Location: United Kingdom
Posts: 42
Posted: 04:43pm 25 Feb 2024
Copy link to clipboard 
Print this post

  lizby said  Try
DIM WIN_PATTERNS(7,2)= (1,2,3,4,5,6,7,8,9,1,4,7,2,5,8,3,6,9,1,5,9,3,5,7)
for i=0 to 2:for j=0 to 7:?WIN_PATTERNS(j,i):next j:next i


Thanks - loads ;-)

Not sure my thinking is correct here.

I am trying to code a basic tic-tac-toe.   I was looking to create an array of arrays (which I don't think I can do).

For example WINNING_PATTERN (1) = "1,4,7"  corresponding to the left most column.  In total there are 8 possible winning patterns.

I think I'll refactor it all and store PLAYER_X as PLAYER_X(8) - where this is intially all zero, and as a player selects a square, that square is added to the array.

Assume the board is;

1 2 3
4 5 6
7 8 9

Player selects 4.  So PLAYER_X(3) = 1 and the PLAYER_X array is 0,0,0,1,0,0,0,0,0

I can then check PLAYER_X(0), PLAYER_X(3) and PLAYER_X(7) to see if they are "1"

I might be going down a rabbit hole though.

Px
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3150
Posted: 04:47pm 25 Feb 2024
Copy link to clipboard 
Print this post

  PEnthymeme said  I was looking to create an array of arrays (which I don't think I can do).


Isn't a 2-dimensional array an "array of arrays"? And by extension more dimensions would be arrays of arrays?
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
LeoNicolas

Guru

Joined: 07/10/2020
Location: Canada
Posts: 479
Posted: 04:54pm 25 Feb 2024
Copy link to clipboard 
Print this post

This is cool. Are you implementing the mini-max algorithm for the game AI?
I have implemented it multiple times in different languages, but never in Basic. I'm curious to know how it will be implemented  
 
PEnthymeme

Regular Member

Joined: 27/12/2023
Location: United Kingdom
Posts: 42
Posted: 04:55pm 25 Feb 2024
Copy link to clipboard 
Print this post

  lizby said  
  PEnthymeme said  I was looking to create an array of arrays (which I don't think I can do).


Isn't a 2-dimensional array an "array of arrays"? And by extension more dimensions would be arrays of arrays?


What I think is going on here is my "Python" head getting in the way.

What I was originally think about was:

WINNING_PATTERN = ((1,4,6), (2,5,8)....)  etc

So that I could access WINNING_PATTERNS(0) and get the array (1,4,6)  - Python-list esque.

I could make WINNING_PATTERNS(0) = 146  as a single entry and match the value.

P
 
PEnthymeme

Regular Member

Joined: 27/12/2023
Location: United Kingdom
Posts: 42
Posted: 04:58pm 25 Feb 2024
Copy link to clipboard 
Print this post

  LeoNicolas said  This is cool. Are you implementing the mini-max algorithm for the game AI?
I have implemented it multiple times in different languages, but never in Basic. I'm curious to know how it will be implemented  


At this point no.

The "AI" (laughingly called at this point) always follows the route:

5 (try the center first), 1, 3, 9,7 (corners clockwise), 2, 6, 8, 4  - if it finds and "empty" spot it goes there.  

And in "text" mode as I like the 1980's Terminal style....

P
Edited 2024-02-26 03:00 by PEnthymeme
 
PEnthymeme

Regular Member

Joined: 27/12/2023
Location: United Kingdom
Posts: 42
Posted: 06:03pm 25 Feb 2024
Copy link to clipboard 
Print this post

Dirty code - it works, but no "error" checking.

FWIW - MMBASIC - Windows -- but nothing "clever" so probably works on VGA etc

OPTION BASE 1

' Display the game title
PRINT TAB(30); "TIC-TAC-TOE"
PRINT TAB(15); " "
PRINT: PRINT: PRINT


' Initialize the game board and the optimal moves sequence
DIM S(9)
DIM MOVES(9) = (5, 1, 3, 7, 9, 2, 4, 6, 8)

' Ask the player to choose their symbol
INPUT "DO YOU WANT 'X' OR 'O'", PLAYER_CHOICE$
IF PLAYER_CHOICE$ = "X" THEN
   P$ = "X"
   Q$ = "O"
ELSE
   P$ = "O"
   Q$ = "X"
END IF

' Print the current state of the game board
SUB PRINT_BOARD
   PRINT " 1 | 2 | 3 "
   PRINT "---+---+---"
   PRINT " 4 | 5 | 6 "
   PRINT "---+---+---"
   PRINT " 7 | 8 | 9 "
   PRINT
   FOR I = 1 TO 9 STEP 3
       FOR J = 0 TO 2
           CELL = I + J
           ' Determine the mark to print based on the cell's state
           IF S(CELL) = 1 THEN
               MARK$ = " " + P$ + " "
           ELSEIF S(CELL) = -1 THEN
               MARK$ = " " + Q$ + " "
           ELSE
               MARK$ = "   "
           END IF
           
           ' Print the cell's content
           IF J < 2 THEN
               PRINT MARK$; "|";
           ELSE
               PRINT MARK$
           END IF
       NEXT J
       ' Print the row separator unless it's the last row
       IF I < 7 THEN PRINT "---+---+---"
   NEXT I
   PRINT: PRINT: PRINT
END SUB

' Initialize the winning patterns
DIM WIN_PATTERNS(8, 3)
DATA 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 4, 7, 2, 5, 8, 3, 6, 9, 1, 5, 9, 3, 5, 7
FOR I = 1 TO 8
   FOR J = 1 TO 3
       READ WIN_PATTERNS(I, J)
   NEXT J
NEXT I

' Check if there's a win, a draw, or if the game should continue
FUNCTION CHECK_WIN()
   FOR I = 1 TO 8
       ' Check for a winning pattern
       IF S(WIN_PATTERNS(I, 1)) <> 0 AND S(WIN_PATTERNS(I, 1)) = S(WIN_PATTERNS(I, 2)) AND S(WIN_PATTERNS(I, 2)) = S(WIN_PATTERNS(I, 3)) THEN
           CHECK_WIN = S(WIN_PATTERNS(I, 1))
           EXIT FUNCTION
       END IF
   NEXT
   
   ' Check for any empty cell to continue the game
   FOR J = 1 TO 9
       IF S(J) = 0 THEN
           CHECK_WIN = 2 ' Game continues
           EXIT FUNCTION
       END IF
   NEXT
   
   ' If no win and no empty cells, it's a draw
   CHECK_WIN = 0
END FUNCTION

' Determine the computer's move based on a predefined sequence
SUB COMPUTER_MOVE
   FOR I = 1 TO 9
       IF S(MOVES(I)) = 0 THEN
           S(MOVES(I)) = -1
           EXIT SUB
       END IF
   NEXT
END SUB

' Main game loop
DO
   ' Print the board and ask for the player's move
   PRINT_BOARD
   DO
       INPUT "WHERE DO YOU MOVE (1-9, 0 to quit): ", M
       IF M = 0 THEN
           PRINT "THANKS FOR THE GAME."
           EXIT DO
       ELSEIF M >= 1 AND M <= 9 AND S(M) = 0 THEN
           S(M) = 1 ' Mark the player's move
           EXIT DO
       ELSE
           PRINT "THAT SQUARE IS OCCUPIED OR INVALID."
       END IF
   LOOP
   
   ' Check the game state after the player's move
   IF CHECK_WIN() <> 2 THEN EXIT DO
   
   ' Computer makes its move
   COMPUTER_MOVE
   
   ' Check the game state after the computer's move
   IF CHECK_WIN() <> 2 THEN EXIT DO
LOOP

' Final board print and game result announcement
PRINT_BOARD
SELECT CASE CHECK_WIN()
   CASE 1
       PRINT "CONGRATULATIONS! YOU WIN!"
   CASE -1
       PRINT "COMPUTER WINS! TRY AGAIN."
   CASE 0
       PRINT "IT'S A DRAW!"
END SELECT


To do:
(1) Error checking
(2) "AI" not based on predetermined moves.
(3) Tidy the screen up

I remember a "StarTrek" game in this manner from a type-in listing from YourSinclair when I was a mere 10-12 year old....

P
Edited 2024-02-26 04:04 by PEnthymeme
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024