The Student Room Group

Aqa comp1 2016

Scroll to see replies

Original post by markhama
finding it almost impossible to do the adjacent move in python, i have everything else done (Saving ect.) just need a hand with this.

@Brodingoson either saving or adjacent move seems like a good last question


Do you mind explaining what you mean by an adjacent move for AQA Reverse?
Original post by Filipo
Do you mind explaining what you mean by an adjacent move for AQA Reverse?


He means that currently you can place a piece anywhere on the board, however in reversi you can only place a piece in a square if it will flip an opponents piece. :smile:
Original post by rockingmax
He means that currently you can place a piece anywhere on the board, however in reversi you can only place a piece in a square if it will flip an opponents piece. :smile:


I see! So the real game is actually called 'reverse'?
Original post by VietDao
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


In the ShowValidMove Function, the variable player is passed ByVal. In my program such a variable does not exist. Can you explain what it does so I can understand how to implement it.
Original post by aelahi23
due to so many requests I'll post the solution here

Step 1) there should be a a function called CheckIfMoveIsValid, what you need to do is modify the if statement to this

If Board[Row, Column] = '"empty space" and(CheckIfThereArePiecesToFlip(Boar d, BoardSize, Row, Column, 1,0 = true
or CheckIfThereArePiecesToFlip(Boar d, BoardSize, Row, Column, -1, 0) = true
or CheckIfThereArePiecesToFlip(Boar d, BoardSize, Row, Column, 0, 1) = true
or CheckIfThereArePiecesToFlip(Boar d, BoardSize, Row, Column, 0, -1) = true)
Then MoveIsValid <== True
what this does is call the function to check if there are pieces to flip and the function will return true or false and if it returns true

Step 2) Make HumanPlayerTurn a global variable instead of a local variable in the PlayGame Function

Step 3)Then paste the following if statement in the function called CheckIfThereArePiecesToFlip right at the start of the function
If HumanPlayersTurn = true
Then Board[StartRow, StartColumn] := 'H'
Else Board[StartRow, StartColumn] := 'C'

when the function to check for flips is called it's called after the letter has been assigned to the game board cells so the flip function doesn't see an empty space but a letter in the place you entered the coordinates for, since we are calling it earlier before the letter gets assigned to the board the function still reads it as an empty space and will not work thats why in step 3 you have to add and if statement to temporarily assign the selected coordinate to hold a letter

if you still don't get it let me know

I have done every other possible problems we discussed on the first page of this thread and have solved them so if anyone is stuck just leave a message here. If I don't do the same language as you I can still provide a working algorithm to solve a problem


With step 3 doesnt this mean that it is inputting the piece onto the coordinate chosen despite it being valid or not? That was what happened to my program after following this... Where have I gone wrong?
Original post by Lemonjiffy
With step 3 doesnt this mean that it is inputting the piece onto the coordinate chosen despite it being valid or not? That was what happened to my program after following this... Where have I gone wrong?


yes there is an error in that. I haven't got it working myself but i think you need to then reset the square back to " " after the function has been used. Like (Board(row,column) = " ") which should get rid of the temp pieces. However i have no clue where to implement this into the code. :frown:
Original post by rockingmax
yes there is an error in that. I haven't got it working myself but i think you need to then reset the square back to " " after the function has been used. Like (Board(row,column) = " ":wink: which should get rid of the temp pieces. However i have no clue where to implement this into the code. :frown:


Yeah I tried this by putting that at the end of "checkIfThereArePiecesToBeFlipped" but this didn't always work since the method is called several times with different directions, therefore if there was a flip in one direction but not in another, the Piece would be overridden by a blank space. I'm wondering if instead of repeatedly checking directions one by one and instead only calling the method once and checking all directions at the same time will fix the mistake?
Original post by Jamesh2303
In the ShowValidMove Function, the variable player is passed ByVal. In my program such a variable does not exist. Can you explain what it does so I can understand how to implement it.

sorry for the late reply, you don't actually need that i put that in there so i could do amendment later when player want to change their initial but you can just do this:
Function ShowValidMove(ByRef board(,) As Char, ByVal boardSize As Integer) 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) = "H" 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

As for where you would implement it, i have implemented in the PlayGame sub just before second do loop and before "DisplayGameBoard(Board, BoardSize)"
Some additional questions:
1) Enhance the program to show what ship has been hit.
2) Radar scan
3) A sea mine. If hit game over.
These are a few I thought of. Hope this helps.
Original post by Kenzar234
Some additional questions:
1) Enhance the program to show what ship has been hit.
2) Radar scan
3) A sea mine. If hit game over.
These are a few I thought of. Hope this helps.


Wrong tgread. This is for the comp1retake
Sorry
Original post by Filipo
I see! So the real game is actually called 'reverse'?


The real game is called Reversi.
Alright, I need some help with certain bits, they are driving me bonkers.

Any thorough algorithms will do just fine.

1) Has anyone managed to implement load game in python which actually works?
2) Adjacent move? I'm going mad over this one, nothing seems to work.

After looking at the game itself, some other possible questions that they may ask are:

1) Allow the player to undo a move.
2) Make the player skip a turn automatically, when there are no valid moves for that player.
3) Show all valid moves that the player can take (idk how this one can be implemented).
4) Maybe an implementation to show the last move of the player?

If anyone has any solutions for the two moves above, I will be very grateful if you could send it to me. :smile:
Original post by Filipo
Alright, I need some help with certain bits, they are driving me bonkers.

Any thorough algorithms will do just fine.

1) Has anyone managed to implement load game in python which actually works?
2) Adjacent move? I'm going mad over this one, nothing seems to work.

After looking at the game itself, some other possible questions that they may ask are:

1) Allow the player to undo a move.
2) Make the player skip a turn automatically, when there are no valid moves for that player.
3) Show all valid moves that the player can take (idk how this one can be implemented).
4) Maybe an implementation to show the last move of the player?

If anyone has any solutions for the two moves above, I will be very grateful if you could send it to me. :smile:


wikibooks has a working solution to the saving and loading questions
https://en.wikibooks.org/wiki/A-level_Computing/AQA/Problem_Solving,_Programming,_Data_Representation_and_Practical_Exercise/Skeleton_code/2016_Exam_Resit


I tried to use that solution, it comes up with an error that the index is out of range :/

EDIT: Nvm, fixed it.
(edited 7 years ago)
Any tips for this exam? How many marks for the actual skeleton programming where you edit it
Original post by Filipo
Alright, I need some help with certain bits, they are driving me bonkers.

Any thorough algorithms will do just fine.

1) Has anyone managed to implement load game in python which actually works?
2) Adjacent move? I'm going mad over this one, nothing seems to work.

After looking at the game itself, some other possible questions that they may ask are:

1) Allow the player to undo a move.
2) Make the player skip a turn automatically, when there are no valid moves for that player.
3) Show all valid moves that the player can take (idk how this one can be implemented).
4) Maybe an implementation to show the last move of the player?

If anyone has any solutions for the two moves above, I will be very grateful if you could send it to me. :smile:


I think this works, but it's a really complicated method has anyone got a simpler one?It basically checks to see wether the piece below, above, left or right or diagonal of the placed piece is empty.I wasn't sure wether diagonal moves using Reversi were allowed, any ideas?Here is my solution anyway, its an edit of the check move is valid FUNCTION(Key word!)
Sorry about indentations, stupid browser puts it like this!

def CheckIfMoveIsValid(Board, Move): MoveIsValid = False Row = Move % 10 Column = Move // 10 if Row != 1 and Row != 6 and Column != 1 and Column != 6: if Board[Row][Column + 1] != " ": MoveIsValid = True elif Board[Row][Column - 1] != " ": MoveIsValid = True elif Board[Row + 1][Column] != " ": MoveIsValid = True elif Board[Row - 1][Column] != " ": MoveIsValid = True elif Board[Row + 1][Column + 1] != " ": MoveIsValid = True elif Board[Row - 1][Column - 1] != " ": MoveIsValid = True elif Row == 1 and Column != 1 and Column != 6: if Board[Row][Column + 1] != " ": MoveIsValid = True if Board[Row + 1][Column] != " ": MoveIsValid = True if Board[Row][Column - 1] != " ": MoveIsValid = True if Board[Row + 1][Column - 1] != " ": MoveIsValid = True if Board[Row + 1][Column + 1] != " ": MoveIsValid = True elif Row == 6 and Column != 1 and Column != 6: if Board[Row][Column - 1] != " ": MoveIsValid = True if Board[Row][Column + 1] != " ": MoveIsValid = True if Board[Row - 1][Column] != " ": MoveIsValid = True if Board[Row - 1][Column - 1] != " ": MoveIsValid = True if Board[Row - 1][Column + 1] != " ": MoveIsValid = True elif Column == 1 and Row != 1 and Row != 6: if Board[Row - 1][Column] != " ": MoveIsValid = True if Board[Row + 1][Column] != " ": MoveIsValid = True if Board[Row][Column + 1] != " ": MoveIsValid = True if Board[Row + 1][Column + 1] != " ": MoveIsValid = True if Board[Row - 1][Column + 1] != " ": MoveIsValid = True elif Column == 6 and Row != 1 and Row != 6: if Board[Row - 1][Column] != " ": MoveIsValid = True if Board[Row + 1][Column] != " ": MoveIsValid = True if Board[Row][Column - 1] != " ": MoveIsValid = True if Board[Row + 1][Column - 1] != " ": MoveIsValid = True if Board[Row - 1][Column - 1] != " ": MoveIsValid = True elif Column == 6 and Row == 6: if Board[Row][Column - 1] != " ": MoveIsValid = True if Board[Row - 1][Column] != " ": MoveIsValid = True if Board[Row - 1][Column - 1] != " ": MoveIsValid = True elif Column == 1 and Row == 1: if Board[Row][Column + 1] != " ": MoveIsValid = True if Board[Row + 1][Column] != " ": MoveIsValid = True if Board[Row + 1][Column + 1] != " ": MoveIsValid = True elif Column == 6 and Row == 1: if Board[Row][Column - 1] != " ": MoveIsValid = True if Board[Row + 1][Column] != " ": MoveIsValid = True if Board[Row + 1][Column - 1] != " ": MoveIsValid = True elif Column == 1 and Row == 6: if Board[Row][Column + 1] != " ": MoveIsValid = True if Board[Row - 1][Column] != " ": MoveIsValid = True if Board[Row - 1][Column + 1] != " ": MoveIsValid = True if Row > BoardSize or Row < 1: MoveIsValid = False elif Column > BoardSize or Column < 1: MoveIsValid = False elif Board[Row][Column] != " ": MoveIsValid = False return MoveIsValid
(edited 7 years ago)
so far i have fixed by code, its up and running from most predictions majority of the people predicted, it is written in visual basic but if you would like to see the full code do pm me.
also do people think this years paper for Section A & Section B would be harder or easier .
Tips and potential questions? marks for editing the skeleton program?
Original post by dianamajek1
so far i have fixed by code, its up and running from most predictions majority of the people predicted, it is written in visual basic but if you would like to see the full code do pm me.
also do people think this years paper for Section A & Section B would be harder or easier .


I think it's going to be easier. They are running of of things to ask. I believe its going to be very broad, they will probably try and touch on every little thing, but I don't think it's going to be anything complex.

Quick Reply

Latest

Trending

Trending