The Student Room Group

Aqa comp1 2016

Scroll to see replies

Hi guys i was wondering as anyone figured out how to save and load the file of the game and how to quit the game mid-way through the game in Visual Basic.
how did you do the following and is it possible by any chance i can see the code i use visual basic but if its python i dont mind.- Skip a turn or quit to menu- Save and Load the Gam
Original post by marioman
No there wasn't. You couldn't even play against the computer in last year's program (Capture The Sarrum).



I must be thinking about the card predict lmao. Computer predicts if the next card is higher or lower e.g.
Original post by Filipo
Just to make sure, I have done the following code changes, is there any problems I might be missing that they may ask?

- Diagonal Flip
- Save and Load the Game
- Validate Coordinates
- Skip a turn or quit to menu
- Return Player score at the end of the game
- Multiplayer
- Player Pieces matching to their names

I'm currently working on allowing a user to undo a move, but are there any other possibilities for this program?


How did you do the skip a turn and quit to menu?
Add validation for player pieces matching to their name. You can't have two players named pater and phi both having p pieces
Original post by KnightCode
I must be thinking about the card predict lmao. Computer predicts if the next card is higher or lower e.g.


Yeah, that was calculating the probability of the next card being higher than the previous card, from June 2014.
Original post by Filipo
Just to make sure, I have done the following code changes, is there any problems I might be missing that they may ask?

- Diagonal Flip
- Save and Load the Game
- Validate Coordinates
- Skip a turn or quit to menu
- Return Player score at the end of the game
- Multiplayer
- Player Pieces matching to their names

I'm currently working on allowing a user to undo a move, but are there any other possibilities for this program?


The undo is a good idea. Could you explain how you skipped/quitted? I do VB so maybe just in pseudocode.

Another one is only allowing the user to take a turn if that turn would take a piece, and skipping it if they can't go. This is a rule in the real board game version of the game
Reply 86
Original post by KnightCode
How did you do the skip a turn and quit to menu?
Add validation for player pieces matching to their name. You can't have two players named pater and phi both having p pieces


Original post by ollycostello
The undo is a good idea. Could you explain how you skipped/quitted? I do VB so maybe just in pseudocode.

Another one is only allowing the user to take a turn if that turn would take a piece, and skipping it if they can't go. This is a rule in the real board game version of the game


In the PlayGame function, after MoveValid = False add:

while not MoveIsValid:
if HumanPlayerTurn:
Skip = ("Would you like to skip the game? N for No, Y for Yes, Q to Quit":wink:

Afterwards you need 3 conditional statements for the option N, Y and Q.

if Skip == N ->
Move = GetHumanPlayerMove(PlayerName)

if Skip == Y ->
HumanPlayerTurn = False
DisplayGameBoard
Move = GetComputerPlayerMove(BoardSize)

if Skip == Q ->
DisplayMenu()
Choice = GetMenuChoice(PlayerName)

else:
Move = GetComputerPlayerMove(BoardSize)
MoveIsValid = CheckIfMoveIsValid(Board, Move)

if not HumanPlayersTurn:
print("Press the Enter key and the computer will make its move")
input()

Hope that helps :wink: Pm me if you don't understand anything
(edited 7 years ago)
Original post by Filipo
In the PlayGame function, after MoveValid = False add:

while not MoveIsValid:
if HumanPlayerTurn:
Skip = ("Would you like to skip the game? N for No, Y for Yes, Q to Quit":wink:

Afterwards you need 3 conditional statements for the option N, Y and Q.

if Skip == N ->
Move = GetHumanPlayerMove(PlayerName)

if Skip == Y ->
HumanPlayerTurn = False
DisplayGameBoard
Move = GetComputerPlayerMove(BoardSize)

if Skip == Q ->
DisplayMenu()
Choice = GetMenuChoice(PlayerName)

else:
Move = GetComputerPlayerMove(BoardSize)
MoveIsValid = CheckIfMoveIsValid(Board, Move)

if not HumanPlayersTurn:
print("Press the Enter key and the computer will make its move":wink:
input()

Hope that helps :wink: Pm me if you don't understand anything



how did you get the cave and load the game i am stock on it could you use pseudocode as i use visual basic
Original post by Filipo
In the PlayGame function, after MoveValid = False add:

while not MoveIsValid:
if HumanPlayerTurn:
Skip = ("Would you like to skip the game? N for No, Y for Yes, Q to Quit":wink:

Afterwards you need 3 conditional statements for the option N, Y and Q.

if Skip == N ->
Move = GetHumanPlayerMove(PlayerName)

if Skip == Y ->
HumanPlayerTurn = False
DisplayGameBoard
Move = GetComputerPlayerMove(BoardSize)

if Skip == Q ->
DisplayMenu()
Choice = GetMenuChoice(PlayerName)

else:
Move = GetComputerPlayerMove(BoardSize)
MoveIsValid = CheckIfMoveIsValid(Board, Move)

if not HumanPlayersTurn:
print("Press the Enter key and the computer will make its move":wink:
input()

Hope that helps :wink: Pm me if you don't understand anything


Think the logic might be wrong with your quit there but obviously different language so I could be wrong (maybe test this and let me know?) If I did this on VB, when I click Q (which would display the menu, if I THEN clicked the "(q)uit game" option at the menu (which is meant to close the program) it wouldn't quit the program, it would go back to the PlayGame function (and possibly try to make the user continue playing).

Edit:
The only way I could think to quit (or skip a turn) which wouldn't have any errors like I predict in yours would be to just have an IF statement before MoveIsValid and GameOver to make a "Q" still be a valid move but which would end the playgame function without displaying that anyone won (another if statement around where it displays who won, IF NOT Coordinates = Q then blahblahblah)
(edited 7 years ago)
Could anyone please pm me the visual basic source code? Thankyou
Original post by archiebaker
Could anyone please pm me the visual basic source code? Thankyou


Its in the wiki linked above
Original post by ollycostello
Its in the wiki linked above


Where sorry, I can't find it :/
Reply 92
Reply 93
Original post by ollycostello
Think the logic might be wrong with your quit there but obviously different language so I could be wrong (maybe test this and let me know?) If I did this on VB, when I click Q (which would display the menu, if I THEN clicked the "(q)uit game" option at the menu (which is meant to close the program) it wouldn't quit the program, it would go back to the PlayGame function (and possibly try to make the user continue playing).

Edit:
The only way I could think to quit (or skip a turn) which wouldn't have any errors like I predict in yours would be to just have an IF statement before MoveIsValid and GameOver to make a "Q" still be a valid move but which would end the playgame function without displaying that anyone won (another if statement around where it displays who won, IF NOT Coordinates = Q then blahblahblah)


Mine works fine, when I'm in the game and press Q, it returns to the main menu. Then if I continue to press (q)uit game, it just prints the menu again. It doesn't quit the program.. hmm thats a very valid point, I will look into that now.

EDIT:

In order to quit the game, an IF statement needs to be implemented inside the IF statement of the option to quit, therefore it will go something like this:

IF Skip == Q ->
DisplayMenu()
Choice = GetMenuChoice(PlayerName)
IF Choice == "q":
quit() // I believe the command for that in VB is close()

EDIT 2:

If you would like the program to quit inside the window, instead of getting a pop up that prompts you to quit, you just do the following:

IF Skip == Q ->
DisplayMenu()
Choice = GetMenuChoice(PlayerName)
IF Choice == "q":
import sys
sys.exit(0) // not too sure what the VB equivalent is, but just look up exiting a VB program internally using appropriate commands, and you should get the answer!


Hope that solved the problem.
(edited 7 years ago)
Reply 94
OK guys i'm making a tutorial (two reason, to help people and making tutorial will also help myself or atlas remind of what I did 2 months ago).

couple of points first of all:
- I'm using VB but i'm also putting up "some what" pseudo code to help others.
- I'm going to assume you guys have at least read the code once.
- I'm aiming to make/fix the game with as little code as possible. reuse the code that AQA given us (make them do all the work).
- anything in square brackets mean it's a function or subroutine.
- anything in curly brackets mean it's a variable.

second, i'm going to talk roughly about the different function in the program:
- There are a few functions in the program that you well NEVER touch upon and there are a few function that you will make a lot of modification.
- The ones that i have never touched are: [CheckIfThereArePieceToFlip], [FlipOpponentPiecesInOneDirection],[PrintLine],[DisplayGameBoard],[GetPlayerName], [SetUpGameBoard]
- The ones that I have modified the most are [GetHumanPlayerMove] and [CheckIfMoveIsValid]. These two function (to me) seem been left very empty and deliberate by AQA.

The Topic I'm going to cover is:
- Validation
- Game Mechanics

Validation:
this can be broken down into three categories:
- out of bound input
- wrong input types (string or chars input when it's only integer)
- invalid game input (i.e. placing a piece down when there is no piece to flip)

---------------------out of bound input
- we are going to use [CheckIfMoveIsValid]
To validate input we are going to need {row}(x-axis) and {column}(y-axis). To do this we check weather the {row} and {column} if greater than board size or smaller than one (i.e. less than 1) then {MoveIsValid} = false. else {MoveIsValid} = true.
here is the code(VB)
If Row > BoardSize Or Row < 1 Or Column > BoardSize Or Column < 1 Then MoveIsValid = False Else If Board(Row, Column) = " " Then MoveIsValid = True End If End If
NOTE: i have nested the given if statement inside my if statement because out of bound validation have a higher priority than the given if statement.

---------------------wrong input types
-we are going to use [GetHumanPLayerMove]
For this i kinda cheated using VB. I used the "Try Catch" function built into VB and recursion. basically the function will try to assign the {coordinate} to user input. if it return a error then it would call the [GetHumanPlayerMove] with same parameters.
Here's the Code(VB)
Function GetHumanPlayerMove(ByVal PlayerName As String) As Integer Dim Coordinates As Integer Try Console.Write(PlayerName & " enter the coordinates of the square where you want to place your piece: " ) Coordinates = Console.ReadLine Catch ex As Exception Console.WriteLine(" invalid input, input only numbers" ) GetHumanPlayerMove(PlayerName) End Try Return Coordinates End Function

---------------------Invalid Game input
- We are going to use [CheckIfMoveIsValid] and [CheckIfThereArePiecesToFlip]
this is actually a lot simpler than most people think, we just have a long if statement nested inside out of bound validation if statement(from previous^).First we place a temporary piece down using player input. Now we called the [CheckIfThereArePiecesToFlip] with parameters of {Board(,)}, {BoardSize}, {StartRow} which is the {row} in [CheckIfMoveIsValid], {StartColumn} which is {column} in [CheckIfMoveIsValid] function, and the direction. the direction is the most trickiest part in this. visually it starts at east (1,0) and ,for me, goes clockwise (i.e. next coordinate would be 1,1). If the if statement return true with when there is at least one direction the piece could flip, then {MoveIsValid} = true.
Here's the code(VB)
If Row > BoardSize Or Row < 1 Or Column > BoardSize Or Column < 1 Then MoveIsValid = False Else If Board(Row, Column) = " " Then MoveIsValid = True If HumanTurn Then Board(Row, Column) = "H" Else Board(Row, Column) = "C" End If If CheckIfThereArePiecesToFlip(Board, BoardSize, Row, Column, 1, 0) Or CheckIfThereArePiecesToFlip(Board, BoardSize, Row, Column, 1, 1) Or CheckIfThereArePiecesToFlip(Board, BoardSize, Row, Column, 0, 1) Or CheckIfThereArePiecesToFlip(Board, BoardSize, Row, Column, -1, 1) Or CheckIfThereArePiecesToFlip(Board, BoardSize, Row, Column, -1, 0) Or CheckIfThereArePiecesToFlip(Board, BoardSize, Row, Column, -1, -1) Or CheckIfThereArePiecesToFlip(Board, BoardSize, Row, Column, 0, -1) Or CheckIfThereArePiecesToFlip(Board, BoardSize, Row, Column, 1, -1) Then MoveIsValid = True Board(Row, Column) = " " Else MoveIsValid = False Board(Row, Column) = " " End If End If End If
NOTE: i have nested this if statement inside the given if statement because it's has the lowest level of priority amongst all other if statement.

Game Mechanics
- allow diagonal flips
- save game
- show where player could move

----------------allow diagonal flips
- we are going to make changes to [MakeMove] by using [FlipOpponentPiecesInOneDirection].
all we have to do here is add four more [FlipOpponentPiecesInOneDirection] with slightly different parameter. why four, because it represent north east, south east, south west, north west.
Here's the code(VB)
'adding diagonal flips
FlipOpponentPiecesInOneDirection(Board, BoardSize, Row, Column, 1, -1)
FlipOpponentPiecesInOneDirection(Board, BoardSize, Row, Column, -1, -1)
FlipOpponentPiecesInOneDirection(Board, BoardSize, Row, Column, -1, 1)
FlipOpponentPiecesInOneDirection(Board, BoardSize, Row, Column, 1, 1)

----------------save game
- we are going to need to create a [save] with parameters of board, board size
- we are going to need to Import System.IO
first we need to assign a path to the file, for me i just used window and copy the directory. then we need to set up a variable as streamwritter (this enable us to write to our file). first we are going to write the board size to our file. then we are going to use 2 for loop, one nested inside each other, to loop through our 2 dimensional array and write the value to the file. then close the file to save it.
Here's the code(VB)
Function save(ByRef board(,) As Char, ByRef BoardSize As Integer)
Dim writer As StreamWriter
Dim name As String name = "\\Mac\Home\Desktop\RESIT\reverse.txt"
writer = New StreamWriter(name) writer.WriteLine(BoardSize)
For i As Integer = 1 To BoardSize
For a As Integer = 1 To BoardSize
writer.WriteLine(board(i, a))
Next
Next writer.Close()
Return "saved" End Function
---------------show valid position where player could move
- we are going to create a [ShowValidMove] with parameter board, board size.
- we are going to use [CheckIfThereArePiecesToFlip] this is quite simple, we mostly use the codes that are already there for us.first create a function , what ever name you like, with the parameter of board, board size. The we need to set up 2 for loop one nested inside each other to loop through our board array. inside our second loop we are going to need s if statement to remove previously placed markers. we are also going to need a if statement to check wether the position on the board is empty. if it is then we will place a temporary move then use the [CheckIfThereArePiecesToFlip] to check wether that move is valid. if it's valid then we place a "X" down at that position on the board else make it empty.
Here's the code (VB)
Function ShowValidMove(ByRef board(,) As Char, ByVal boardSize As Integer, ByVal player As Char)
Dim x As Integer = 0
For i As Integer = 1 To boardSize
For a As Integer = 1 To boardSize
If board(i, a) = "X" Then
board(i, a) = " "
End If
If board(i, a) = " " Then
board(i, a) = player
If FlipOpponentPiecesInOneDirection(board, boardSize, i, a, 1, 0, True) Or FlipOpponentPiecesInOneDirection(board, boardSize, i, a, 1, -1, True) Or FlipOpponentPiecesInOneDirection(board, boardSize, i, a, -1, -1, True) Or FlipOpponentPiecesInOneDirection(board, boardSize, i, a, -1, 0, True) Or FlipOpponentPiecesInOneDirection(board, boardSize, i, a, -1, 1, True) Or FlipOpponentPiecesInOneDirection(board, boardSize, i, a, 0, 1, True) Or FlipOpponentPiecesInOneDirection(board, boardSize, i, a, 1, 1, True) Or FlipOpponentPiecesInOneDirection(board, boardSize, i, a, 0, -1, True) Then board(i, a) = "X"
x += 1
Else board(i, a) = " "
End If
End If
Next
Next
Return x
End Function
(edited 7 years ago)
As it you can only move if you are going to take an adjacent piece, i think you did the VB code for this but transferring it to python 3.4 has proven to be quite difficult
Reply 96
Original post by markhama
As it you can only move if you are going to take an adjacent piece, i think you did the VB code for this but transferring it to python 3.4 has proven to be quite difficult


as far as i can tell python skeleton code looks very similar to VB code.
-----------
def MakeMove(Board, BoardSize, Move, HumanPlayersTurn): Row = Move % 10 Column = Move // 10 if HumanPlayersTurn: Board[Row][Column] = "H" else: Board[Row][Column] = "C"
FlipOpponentPiecesInOneDirection(Board, BoardSize, Row, Column, 1, 0)
FlipOpponentPiecesInOneDirection(Board, BoardSize, Row, Column, -1, 0)
FlipOpponentPiecesInOneDirection(Board, BoardSize, Row, Column, 0, 1)
FlipOpponentPiecesInOneDirection(Board, BoardSize, Row, Column, 0, -1)
FlipOpponentPiecesInOneDirection (Board, BoardSize, Row, Column, 1, -1) FlipOpponentPiecesInOneDirection (Board, BoardSize, Row, Column, -1, -1) FlipOpponentPiecesInOneDirection (Board, BoardSize, Row, Column, -1, 1) FlipOpponentPiecesInOneDirection (Board, BoardSize, Row, Column, 1, 1)
-----------
try and copy this into your python code and run it see if it work or not. It should work because the VB code and python are really similar
How do you implement the save game function into the program itself. Where is the function called from??
Reply 98
Original post by Jamesh2303
How do you implement the save game function into the program itself. Where is the function called from??

the answer to that question is that it's awkward. you can either make it ask to save everytime you make a move ,which mean you would called it in the do loop in PlayGame sub. you could make it so user would go to main menu then save from there. There is no finesse way of saving the game. loading is easy since you would ask user to load before the do loop in the PlayGame sub.
personally i don't think they're are going to ask you to save or load the game because of the reason of where are you going to call the function from.
(edited 7 years ago)
Reply 99
Has anyone managed to implement the LoadGame function? I've wrote mine, but I keep getting an error that the index is out of range.. If anyone has a working solution in python, please pm me :wink:

Quick Reply

Latest

Trending

Trending