News:

Printed Amstrad Addict magazine announced, check it out here!

Main Menu

Switching names from one list to another (simple programming help needed)

Started by DRobert, 19:02, 15 November 24

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

DRobert

As with previous posts you guys on here have been brilliant in helping me to develop my simple football game on Amstrad BASIC (its all text based strategy not graphics which are beyond me at this stage).   As a rank amateur I've achieved some success but I nearly always end up writing dozens of lines with goto's and gosub's to do something which should be relatively simple.  Please be patient as I ask for a bit more help in my task.

I have about 25 players and occasionally the game player needs to switch (transfer players / change the team etc).  Its easy enough to write lines to switch a few players but when it comes to the possibility of switching larger numbers the possibilities are there but the 'long hand' method I use is too cumbersome and takes up too much of my limited space on my program.

If say a goalkeeper is labelled as "gk1$" and I wish to switch him with another goalkeeper in the squad ie "gk12$" I can do a simple program to switch those two.
It maybe the game player needs to switch a defender "df2$" with a midfield player "mf6$" or whatever.  

However if I have 25 players and any one of them may be switched with another the amount of typing is beyond my limited skills. 

At the moment I have two columns on my game screen.

On the Left is a list of player names numbered 1 to 11 and on right hand side a further list with reserve players numbered 12 to 25.   I simply needed to be able switch any one from one list to the other.

If anyone can provide me with a few lines of code to set me on my way I would be very grateful.   Ive spent most of my spare time this week try to sort but cant achieve my aims.

Regards



DRobert 


Jean-Marie

You'd do like this to switch 2 players' names :
10 DIM player$(24)  'player$(0) is the 1st element
20 REM FILL PLAYER'S NAMES
30 FOR x=0 TO 24:READ player$(x):NEXT
40 DATA A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y
50 REM SWITCH PLAYERS 3 AND 20
60 temp$=player$(20)
70 player$(20)=player$(3)
80 player$(3)=temp$

lightforce6128

Use arrays instead of a bunch of numbered variables! The DIM command creates an array. While e.g. "a$" is a single variable, "DIM a$(24)" will create 25 variables with the names "a$(0)" up to "a$(24)". The clue: This is not just a different way to write variable names, but you can use another variable with a number to select one of the so defined variables:

100 ' Create the array of variables.
110 DIM name$(24)
120 ' Somehow fill it with content, e.g. by direct assignment or from a file.
130 name$(0)="name1"
140 name$(1)="name2"
150 ' ...
160 name$(24)="name25"
170 ' Show the players.
180 GOSUB 300
190 ' Ask for which players to switch.
200 LOCATE 1,16:INPUT "Switch which players:",a,b
210 ' Adapt numbers from 1... to 0...
220 a=a-1:b=b-1
230 ' Switch with a helper variable.
240 helper$=name$(a)
250 name$(a)=name$(b)
260 name$(b)=helper$
270 ' Show the players again.
280 GOSUB 300
290 END
300 ' Print players divided in two columns.
310 CLS
320 FOR i=0 TO 10:LOCATE 1,i+1:PRINT i+1;" - ";name$(i):NEXT
330 PRINT
340 FOR i=11 TO 24:LOCATE 20,i-11+1:PRINT i+1;" - ";name$(i):NEXT
350 RETURN

When asked, enter e.g. "1,25" to switch the first and the last player.

lightforce6128

@Jean-Marie : You were faster ...

Jean-Marie

This one associates a rank to each player, and switch both the name & the rank.
10 DIM player$(24),rank$(24)  'player$(0) is the 1st element
20 REM FILL PLAYER'S NAMES
30 FOR x=0 TO 24:READ player$(x):NEXT
40 FOR x=0 TO 24:READ rank$(x):NEXT
50 DATA A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y
60 DATA GK,GK,DF,DF,DF,DF,DF,DF,DF,MF,MF,MF,MF,MF,MF,MF,MF,MF,MF,ST,ST,ST,ST,ST,ST
70 REM Switch players 3 & 20
80 a=3:b=20:GOSUB 120
90 PRINT player$(3);" ";rank$(3):PRINT player$(20);" ";rank$(20)
100 END
110 REM Switch 2 players a & b (name & rank)
120 temp$=player$(b)
130 player$(b)=player$(a)
140 player$(a)=temp$
150 temp$=rank$(b)
160 rank$(b)=rank$(a)
170 rank$(a)=temp$
180 RETURN

Jean-Marie

This example shows the transfer of a player between 2 teams.
Since you manage several teams (2 in this example), we'll have to use arrays of 2 dimensions : player$(x,25) where x is the number of the team. and 25 is the name of the last player.
Note that I have decided to fill the data from element(1) rather than element(0).
This will waste a bit of memory since elements(0) will never be used, but make things simpler.
10 DIM player$(2,25),rank$(2,25)  'replace 2 by the teams number
20 REM Fill data for each team
30 FOR team=1 TO 2
40 FOR x=1 TO 25:READ player$(team,x):NEXT
50 FOR x=1 TO 25:READ rank$(team,x):NEXT
60 NEXT team
70 REM Data for Team 1
80 DATA A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y
90 DATA GK,GK,DF,DF,DF,DF,DF,DF,DF,MF,MF,MF,MF,MF,MF,MF,MF,MF,MF,ST,ST,ST,ST,ST,ST
100 REM Data for Team 2
110 DATA A2,B2,C2,D2,E2,F2,G2,H2,I2,J2,K2,L2,M2,N2,O2,P2,Q2,R2,S2,T2,U2,V2,W2,X2,Y2
120 DATA MF,MF,MF,MF,MF,MF,MF,MF,MF,MF,DF,DF,DF,DF,DF,DF,DF,GK,GK,ST,ST,ST,ST,ST,ST
130 REM Switch Player 3 from Team 1 with Player 20 from Team 2
140 player1=3:team1=1:player2=20:team2=2:GOSUB 180
150 PRINT player$(1,3);" ";rank$(1,3):PRINT player$(2,20);" ";rank$(2,20)
160 END
170 REM Switch 2 players
180 temp$=player$(team2,player2)
190 player$(team2,player2)=player$(team1,player1)
200 player$(team1,player1)=temp$
210 temp$=rank$(team2,player2)
220 rank$(team2,player2)=rank$(team1,player1)
230 rank$(team1,player1)=temp$
240 RETURN

DRobert

To Jean-Marie and Lightforce 6128.  

Thank you both for the time and effort you have put in to helping me with my programme.  I've been lazy with my programming and have not used ARRAYS and the FOR / NEXT functions probably because I've not understood their capability.   I've read about them in the past so Im now practising a few simple exercises to gain understanding.

You guys are obviously light years ahead of me on this sort of thing so I appreciate you 'dumbing down' to my level to assist.  

Kind regards to you and all others on this forum.


Powered by SMFPacks Menu Editor Mod