The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.


Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 32

Thread: Gomoku (5 in a row) board game.

  1. #16
    Ok great so I have this now.

    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;
    }
    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?

  2. #17
    1,391
    posts
    Registered User
    ok - 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;
    }

  3. #18
    Ha 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 -
    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)
    This sounds like the most complicated part. Could you show me how this might be done? Last favour I swear.

    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.

  4. #19
    1,391
    posts
    Registered User
    ok - 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

  5. #20
    Ha you're possibly the nicest Internet person I've ever met. Ok I'll give it a shot.

  6. #21
    1,391
    posts
    Registered User
    lol - 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?

  7. #22
    Hey 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.

  8. #23
    1,391
    posts
    Registered User
    ok - i'll try to help you along - good work - post when you've gotten through the implementations above - i'll keep checking back

  9. #24
    So are you saying that I'll need to write conditional statements for every single one of the 225 squares in every direction?

  10. #25
    1,391
    posts
    Registered User
    not 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

    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?
    ok - that's close - IF your looking for the horizontals - rem that 'row' refers to the horizontal, column the vertical

    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 row
    Last edited by cbeech; May 11th, 2012 at 03:15 PM.

  11. #26
    1,391
    posts
    Registered User
    see 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...

  12. #27
    I 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.
    Code:
    function CheckWin(){
        for (var row:int=0; row<mainBoard.length; row++) {
            for (var col:int=0; col<mainBoard[row].length; col++) {
            
                }
            }
    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.

  13. #28
    1,391
    posts
    Registered User
    lol - 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++) {
            
            }
        }
    }

  14. #29
    Ha 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.

  15. #30
    1,391
    posts
    Registered User
    oh 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
    		}

Page 2 of 3 FirstFirst 123 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012