Results 16 to 30 of 32
Thread: Gomoku (5 in a row) board game.
-
May 10th, 2012, 03:25 PM #1616Registered User
postsOk great so I have this now.
Which allows me to place yellow squares on the board for player 1. How would I then swap to player 2 and place a differently coloured tile on the board, then swap back to player 1?Code:function PlayerClick(Evt:MouseEvent):void { var playerOne:int = 1; var playerTwo:int = 2; mainBoard[Evt.currentTarget["row"]][Evt.currentTarget["column"]] = 1 var myColor:ColorTransform = new ColorTransform(); myColor.color = 0xFFF000; Evt.currentTarget.transform.colorTransform = myColor; }
-
May 10th, 2012, 05:10 PM #171,391Registered User
postsok - so what i might do here is to declare some of those elements outside the methods - then they can be used to help track and assign - you'd probably want to also have an advancing mechanism each time a play is made so perhaps something along these lines:
Code:var playerOne:int = 1; var playerTwo:int = 2; var currentPlayer:int = 1; var playerOneColor:int = 0xFFF000; var playerTwoColor:int = 0x0000FF; var myColor:ColorTransform = new ColorTransform(); function PlayerClick(Evt:MouseEvent):void { //assign data mainBoard[Evt.currentTarget["row"]][Evt.currentTarget["column"]] = currentPlayer; //change colors myColor.color = ( currentPlayer == 1 ) ? playerOneColor : playerTwoColor; Evt.currentTarget.transform.colorTransform = myColor; //change players currentPlayer = ( currentPlayer == 1 ) ? 2 : 1; }
-
May 10th, 2012, 05:40 PM #1816Registered User
postsHa that's so cool, I need to study this stuff more.
So the last thing I need to do is the win check. You said earlier -
This sounds like the most complicated part. Could you show me how this might be done? Last favour I swear.as for the win check, you'll need to develop an algorithm that checks for contiguous paths in the 8 possible directions from a grid point of origin, each time a piece is played you'd want to run the check - you would find the neighboring index in the array in each direction, if it contains the correct piece, then move to the next neighbor in the same direction and continue, count each iteration step that is linked, when the total equals the winning amount (ie. 5 ) the game is won by that player - you would perform this check for every position where there is an occupied game piece (i realize that there may be better ways to store the data for checking, but this is just basic brute force principles here)
I take it I'm meant to create the loop to scan the array inside the playerClick
function.
So i understand I'm meant to temp_chain_length++ whenever another coloured square is found in any direction next to the player square that has been clicked. How would I check that the square next to the player square is occupied? A series of switch statements?Last edited by Iamgavin; May 11th, 2012 at 04:50 PM.
-
May 11th, 2012, 10:40 AM #191,391Registered User
postsok - very good you're on the right track - since this is for a graded course, i've been hesitant to throw out too many solutions, but you are really working hard - kudos
on both counts you have the right idea, we need to track the chain, and return true if we find one that equals the range - this is going to be a long algorithm required to run through each directional path from each board location, i would suggest that you set up an method specifically for this operation - then you call that method from the click handler
the next phase will involve setting up loops to calculate the locations along each of the 8 possible paths from any grid location (ie. row/column)
get the method set up, move the loops and we'll continue on from there
-
May 11th, 2012, 10:47 AM #2016Registered User
postsHa you're possibly the nicest Internet person I've ever met. Ok I'll give it a shot.
-
May 11th, 2012, 12:32 PM #211,391Registered User
postslol - well idk bout that
- but thanks - how's it coming? and deadline? are you running out of time?
for the next phase, grab a sheet of paper or something, draw a simple grid - doesn't matter how many blocks it has, but use enough so that you can see the patterns you'll make (perhaps at least 5x5) - number the grid horizontally and vertically like a graph but from the top left corner starting with 0 for both directions - this represents the rows (vert) and columns (horz) that are in the array with the numbers indicating their positions
pick a point on the grid to start, examine the sequence of indexes required for a vertical UP chain from the starting position - what do we need to do?
-
May 11th, 2012, 01:15 PM #2216Registered User
postsHey I'm just finishing off a 2000 word evaluation report I need to include along with it. Should be finished that soon. Then back to the checkwin problem. Figured I should get the writing out the way whilst my brain was (relatively) fresh. Deadline is in just under 6 hours.
Okay so the numbers in the array are basically the same as x y coordinates. So going down the first row it would be 0,0 0,1 0,2 0,3 0,4 0,5 0,6 etc So to go up the way you would check if the column index is one less than the selected square's column index? Left and right the index is the same, and downwards it has one added to it?Last edited by Iamgavin; May 11th, 2012 at 02:11 PM.
-
May 11th, 2012, 02:02 PM #231,391Registered User
postsok - i'll try to help you along - good work - post when you've gotten through the implementations above - i'll keep checking back
-
May 11th, 2012, 03:08 PM #2416Registered User
postsSo are you saying that I'll need to write conditional statements for every single one of the 225 squares in every direction?
-
May 11th, 2012, 03:11 PM #251,391Registered User
postsnot quite - you'll loop through them with the nested loop statements - but within those, you'll need to loop through each of the 8 paths for each position
opps - missed the above append - reading
ok - that's close - IF your looking for the horizontals - rem that 'row' refers to the horizontal, column the verticalOkay so the numbers in the array are basically the same as x y coordinates. So going down the first row it would be 0,0 0,1 0,2 0,3 0,4 0,5 0,6 etc So to go up the way you would check if the column index is one less than the selected square's column index? Left and right the index is the same, and downwards it has one added to it?
row0 [ column0, column1, etc ]
row1 [ column0, column1, etc ]
row2 [ column0, column1, etc ]
etc
so the first loop run through the rows, the second through each column in that rowLast edited by cbeech; May 11th, 2012 at 03:15 PM.
-
May 11th, 2012, 03:20 PM #261,391Registered User
postssee it's not just the one less though - we need to track the path over the range (ie 5) so on the first iteration it will become: column-1, column-2, column-3, column-4 - this moving horizontally to the left - so to move to the right we'd use...?
similarly, for a vertical run up or down, but instead of decrementing/incrementing the 'column' value, we use the row value
however, your going to see a problem with this shortly, after looking at your scratch grid and walking through the numbers...
-
May 11th, 2012, 03:45 PM #2716Registered User
postsI think I'm going slightly mad after thinking about this all day/all week, not sure I'll be able to work this out with 3 hours to go.
So i have this function.
I'm not sure how to refer to the position of the player piece within the array let alone check in every direction. I get why you don't want to give any more straight solutions but my brain is fried.Code:function CheckWin(){ for (var row:int=0; row<mainBoard.length; row++) { for (var col:int=0; col<mainBoard[row].length; col++) { } }
-
May 11th, 2012, 04:07 PM #281,391Registered User
postslol - i feel ya
- i understand, i'll also be running out shortly here for a bit i'm afraid
ok - so you've got the method set - now i would add some other properties that you'll need throughout the course of the check: ROWS, COLUMNS, RANGE - each an int and each as a class scope property
next, inside the method body, you'll want to set some local properties also for use throughout the loops we'll set up - i have them as: i (for index), c (for count)
then, you can adjust the loops to read:
Code:var ROWS:int = 15; var COLUMNS:int = 15; var RANGE:int = 5; function CheckWin(){ var i:int; var c:int; for (var row:int=0; row<ROWS; row++) { for (var col:int=0; col<COLUMNS; col++) { } } }
-
May 11th, 2012, 04:27 PM #2916Registered User
postsHa no worries thanks for all your help. I couldn't finish it in time so I just submitted it the way it is. Hopefully got enough in there to pass. Think I might take a break fom flash for a while lol.
Last edited by Iamgavin; May 11th, 2012 at 05:56 PM.
-
May 11th, 2012, 06:22 PM #301,391Registered User
postsoh shoot - sorry to duck out - ok well if your prof has trouble, point him my way, i'll back your efforts

but for the sake of completing this thread (and perhaps you can submit) i'll post the remainder of the algorithm of the check sequence - you can study it to see how it works - i think you get the concept of needing to loop through the sequence(s) - what did not yet discuss is 'limiting' the range of the derived index to values within the array range were searching - this condition must also be satisfied or throw errors - we also need to 'count' the chain, and break out of the method if we find one that is complete (the concept is that you will know 'who' is the winner according to who the 'current player' is set to at the time of making the check) - there is more to discuss, and it's not particularly simple
sorry again to not get you there in time - good effort
Code:public function check():Boolean { var i:int; var c:int; for( var row:int=0; row<ROWS; row++ ) { for( var col:int=0; col<COLUMNS; col++ ) { //CHECK for player index if( mainBoard[row][col] != player ) continue; //CHECK UP for( i=1,c=1; i<RANGE; i++ ) { if( row-i < 0 ) break; if( mainBoard[row-i][col] != player ) break; if( ++c == RANGE ) return true; } //CHECK DOWN for( i=1,c=1; i<RANGE; i++ ) { if( row+i >= ROWS ) break; if( mainBoard[row+i][col] != player ) break; if( ++c == RANGE ) return true; } //CHECK LEFT for( i=1,c=1; i<RANGE; i++ ) { if( col-i < 0 ) break; if( mainBoard[row][col-i] != player ) break; if( ++c == RANGE ) return true; } //CHECK RIGHT for( i=1,c=1; i<RANGE; i++ ) { if( col+i >= COLUMNS ) break; if( mainBoard[row][col+i] != player ) break; if( ++c == RANGE ) return true; } //CHECK UP/RIGHT for( i=1,c=1; i<RANGE; i++ ) { if( row-i < 0 || col+i >= COLUMNS ) break; if( mainBoard[row-i][col+i] != player ) break; if( ++c == RANGE ) return true; } //CHECK DOWN/RIGHT for( i=1,c=1; i<RANGE; i++ ) { if( row+i >= ROWS || col+i >= COLUMNS ) break; if( mainBoard[row+i][col+i] != player ) break; if( ++c == RANGE ) return true; } //CHECK UP/LEFT for( i=1,c=1; i<RANGE; i++ ) { if( row-i < 0 || col-i < 0 ) break; if( mainBoard[row-i][col-i] != player ) break; if( ++c == RANGE ) return true; } //CHECK DOWN/LEFT for( i=1,c=1; i<RANGE; i++ ) { if( row+i >= ROWS || col-i < 0 ) break; if( mainBoard[row+i][col-i] != player ) break; if( ++c == RANGE ) return true; } } } return false; //no winner }

Reply With Quote


Bookmarks