Original post by VietDaoOK 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