Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 whatdotcolor
Index -> Programming, Java -> Java Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
GoVikingsGo




PostPosted: Wed Jan 11, 2006 10:01 am   Post subject: whatdotcolor

For my final project I am making connect four in java. My plan is too use console. I need a command or method similar to whatdot color in java and how to use it. The idea is to check if the four tokens are in a row using this command

Any help would be great thanks alot
Sponsor
Sponsor
Sponsor
sponsor
evogre3n




PostPosted: Mon Jan 16, 2006 7:56 am   Post subject: (No subject)

Hey howard,

Use and array to store the information..

and lets say.. you want to check if there are 4 in a row like you said in your post...

Then just run through a loop that checks 4 array locations in a row etc.

Then draw the things based on the array information.. so lets say value of
'1' means that place is taken.. and '0' means that place isnt taken... then run througha loop.. that checks if the value of the array location is 1, then draw a circle or "token" there.. otherwise, dont draw anything.

good luck[/code]
Hikaru79




PostPosted: Mon Jan 16, 2006 9:22 pm   Post subject: (No subject)

Or, even better, let's realize that we're using JAVA, which is OBJECT-ORIENTED, and taking care of this stuff is EXACTLY why it was designed -- to give you a clean way to model things like this without having to hack away with arrays and whatdotcolor. If you're this far in the year of a Java course and still don't have an almost knee-jerk reaction to make an object-oriented model of this sort of thing, your teacher is likely incompetent.
evogre3n




PostPosted: Mon Jan 16, 2006 10:20 pm   Post subject: (No subject)

Well then

feel free to help out..

not really appreciated if you come in here saying "ooo OOP is amazing.. USe iT TO SOLVE THIS"

and then just leave it at that

please explain how to go about solving his problem using your proposed method
wtd




PostPosted: Mon Jan 16, 2006 10:28 pm   Post subject: (No subject)

You have some data structure (like a two-dimensional array, for instance) that maintsin the state of your game. Then you have code that draws that state.

You also have code which recognizes that a user has pressed some key. This code changes the state of the game, then redraws it.
wtd




PostPosted: Mon Jan 16, 2006 10:29 pm   Post subject: (No subject)

Since you have the state of your game in some kind of data structure, you can easily use that information to check for used spaces, rather than some "whatdotcolor"-ish hack.
Hikaru79




PostPosted: Mon Jan 16, 2006 10:45 pm   Post subject: (No subject)

evogre3n wrote:
Well then

feel free to help out..

not really appreciated if you come in here saying "ooo OOP is amazing.. USe iT TO SOLVE THIS"

and then just leave it at that

please explain how to go about solving his problem using your proposed method


Alright, well, the Swing part of it is too large for me to cover it in one post (in fact, it is more appropriately covered in an entire book), but you can look up what you need about that in http://sun.java.com .

As for the game model, sure, I'll help you a bit with that Smile

Java:

public class GamePiece
{

    private boolean color; //true = black, false = red
   
    public GamePiece(boolean color) {
          this.color = color;
    }
}


Java:

public class GameSlot
{

     GamePiece occupyingPiece;

     public GameSlot() {
     }

     public boolean isOccupied() {
         return occupyingPiece!=null;
      }

      public void occupy(GamePiece piece) {
           occupyingPiece = piece;
      }
}


Java:

import java.util.ArrayList;

public class GameBoard
{

     GameSlot [] [] grid;
     ArrayList<GamePiece> pieceList;
     
     public GameBoard (int x, int y) {
          grid = new GameSlot [x] [y];
          for (int r = 0; r < x; r++) {
               for (int c = 0; c < y; c++) {
                    grid [r][c] = new GameSlot();
                }
           }
          pieceList = new ArrayList<GamePiece>();

       public int dropPiece(boolean turn, int c) {
            int r = grid[0].length;
            while (!grid [c][r].isOccupied()) {
                 r -= 1;
            }
            grid[c][r+1].occupy(new GamePiece(turn));
            return r+1;   
       }
}


This is very rough, incomplete, and probably has a bug or two. But this is the idea you should be following Smile Let me know if you have any other questions, and sorry for being short earlier. It's not you I was mad it, its your teacher, for giving you guys assignments that BEG to be done properly, but not giving you the right tools.
GoVikingsGo




PostPosted: Tue Jan 17, 2006 8:52 am   Post subject: (No subject)

okay thanks I still think im gonna go about using the array instead of that whatdotcolor - ish thing THANKS!
Sponsor
Sponsor
Sponsor
sponsor
Hikaru79




PostPosted: Tue Jan 17, 2006 4:02 pm   Post subject: (No subject)

GoVikingsGo wrote:
okay thanks I still think im gonna go about using the array instead of that whatdotcolor - ish thing THANKS!


Okay, I'm glad we at least dissuaded you from using whatdotcolor Smile Now keep in mind that the code I posted is pretty much also just an array, but its all kept as objects to make the code easier to write, debug and maintain. But just having an int[][] works too.

Good luck with the project Smile Ask here for more help if you need it.
GoVikingsGo




PostPosted: Tue Jan 17, 2006 6:29 pm   Post subject: (No subject)

Okay thanks alot, my idea is a 2d array storing 1 or 0 to indicate whether or not the spot is filled....

Now i need to figure out how to check if the player has won....


Thanks to all... Very Happy
Hikaru79




PostPosted: Tue Jan 17, 2006 6:37 pm   Post subject: (No subject)

GoVikingsGo wrote:
Okay thanks alot, my idea is a 2d array storing 1 or 0 to indicate whether or not the spot is filled....

Okay, but then how do you know if the piece filling it belongs to the red player or the black player? Smile
GoVikingsGo




PostPosted: Tue Jan 17, 2006 10:18 pm   Post subject: (No subject)

if complicates things a bit but 1 means player 1 and 2 means player 2

Cool
GoVikingsGo




PostPosted: Tue Jan 17, 2006 10:23 pm   Post subject: (No subject)

okay one more question Embarassed

i have this

code:

 for (int col = 1 ; col < 7 ; col++)
            {
                if (memVar [col] [1] == 1 || memVar [col] [1] == 2)
                {
                    c.fillOval (25, 120, 50, 50);
                }
                else if (memVar [col] [2] == 1 || memVar [col] [2] == 2)
                {
                    c.fillOval (25, 182, 50, 50);
                }
                else if (memVar [col] [3] == 1 || memVar [col] [3] == 2)
                {
                    c.fillOval (25, 244, 50, 50);
                }
                else if (memVar [col] [4] == 1 || memVar [col] [4] == 2)
                {
                    c.fillOval (25, 306, 50, 50);
                }
                else if (memVar [col] [5] == 1 || memVar [col] [5] == 2)
                {
                    c.fillOval (25, 368, 50, 50);
                }
                else
                {
                    c.fillOval (25, 430, 50, 50);
                }
            }


the idea is to redraw the tokens everytime my loop runs but when it hits the first if statement it stop eventhough row 1, 4 and 5 may be full, Any ideas?
Hikaru79




PostPosted: Tue Jan 17, 2006 10:34 pm   Post subject: (No subject)

GoVikingsGo wrote:
okay one more question Embarassed

i have this

code:

 for (int col = 1 ; col < 7 ; col++)
            {
                if (memVar [col] [1] == 1 || memVar [col] [1] == 2)
                {
                    c.fillOval (25, 120, 50, 50);
                }
                else if (memVar [col] [2] == 1 || memVar [col] [2] == 2)
                {
                    c.fillOval (25, 182, 50, 50);
                }
                else if (memVar [col] [3] == 1 || memVar [col] [3] == 2)
                {
                    c.fillOval (25, 244, 50, 50);
                }
                else if (memVar [col] [4] == 1 || memVar [col] [4] == 2)
                {
                    c.fillOval (25, 306, 50, 50);
                }
                else if (memVar [col] [5] == 1 || memVar [col] [5] == 2)
                {
                    c.fillOval (25, 368, 50, 50);
                }
                else
                {
                    c.fillOval (25, 430, 50, 50);
                }
            }


the idea is to redraw the tokens everytime my loop runs but when it hits the first if statement it stop eventhough row 1, 4 and 5 may be full, Any ideas?


I may be misunderstanding your question, but if I'm not, all you have to do is remove the "else". Just have disjoint if statements. That way, even if one of them is true, it will still evaluate the rest anyway.

Also, a tip:
code:
else if (memVar [col] [5] == 1 || memVar [col] [5] == 2)

can just be written as
code:
else if (memVar [col] [5] > 0)

Smile Easier to understand what's going on, eh?
GoVikingsGo




PostPosted: Tue Jan 17, 2006 10:41 pm   Post subject: (No subject)

no i cant cause its different each time.... thx solved my problem
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 22 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: