The Artima Developer Community
Sponsored Link

Java Answers Forum
Connect4 HELP HELP HELP

1 reply on 1 page. Most recent reply: Mar 20, 2002 4:58 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Patti

Posts: 46
Nickname: patti
Registered: Feb, 2002

Connect4 HELP HELP HELP Posted: Mar 19, 2002 6:38 PM
Reply to this message Reply
Advertisement
Does anyone know of a game called Connect4, where you drop "pieces" down columns until you get four in a row either across, up&down or diag? I'm supposed to design a program for this game and I'm REALLY in over my head this time. I can barely get going. Can someone help? This is what I have (I'm embarrassed to show this)I don't even understand how a player can "drop" a "piece" "down" a column. We haven't even learned how to change players. All help appeciated.


import java.io.*;
 
 
class Connect4 {
	
	public final static int NOPLAYER = 0;
	public final static int PLAYER1 = 1;
	public final static int PLAYER2 = 2;
	
	//--------------------------------------------------------
	// Main driver for the game.
	//--------------------------------------------------------
	public static void main (String[] args) throws Exception {
		
		boolean again = true;
		int player = PLAYER2;
		int winner = NOPLAYER;
		int totalmoves = 0;
		int totalgames = 0;
		
		Board board;
		
		BufferedReader stdin = new BufferedReader
		 (new InputStreamReader (System.in));
		 
		 while (again) {
		 	
		 	board = new Board();
		 	
		 	winner = NOPLAYER;
		 	
		 	while (winner == NOPLAYER) {
		 		
		 		player = (player == PLAYER1) ? PLAYER2 : PLAYER1;
		 		
		 		winner = board.check_win();
		 		
            }
            
            System.out.println ("Congratulations Player " + winner + "!!!");
            
            
            System.out.println ("Number of moves: " + totalmoves);
            System.out.println ("Number of games: " + totalgames);
            
            again = false;
            System.out.print ("Do you want to play another game (y/n)? ");
            again = stdin.readLine().equals ("y");
             }
             
  } //method main
} //class Connect4
 
//-------------------------------------------------------------------
// Represents one player piece.
//-----------------------------------------------------------------
 
class Piece {
    	
    	private int owner;
    	
    	//*******************************************************
    	// Sets up a new piece, owned by neither player.
    	//*******************************************************
    	
   public Piece() {
    		
    	owner = Connect4.NOPLAYER;
    		
          	                  
    	if (owner == 0){
    	 System.out.println (" ");
    	if (owner == 1)
    	 System.out.println ("X");
    	if (owner == 2)
    	 System.out.println ("O");}
    	
    	} // constructor Piece
    		    	
  } //class Piece
  
  //**************************************************************
  // Represents a game board.
  //**************************************************************
  
 class Board {
      	          
   	    int[][]piece = new int[8][8];
   	    
   	    
   	public Board ()
   	 {
   	   
          // Print the board
                          
         for (int columns=0; columns < piece.length; columns++);{         
  	      	
  	      	System.out.println(" 0 1 2 3 4 5 6 7"); 
  	     
  	      	
  	      for (int row = 0; row < piece.length; row++)
  	      
            System.out.println("| | | | | | | | |");        
  	    }      	                  
             
     }
      	//--------------------------------------------------------
      	// Checks if there are any winners at this point.
      	//--------------------------------------------------------
      	      	
    public int check_win() {
      		
      		int result = Connect4.NOPLAYER;
      		
      		return result;
      		
      	} //method check_win
      	
  } // class Board
  	


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Connect4 HELP HELP HELP Posted: Mar 20, 2002 4:58 PM
Reply to this message Reply
On such a problem, which seems complicated at first, the trick is to decompose it into smaller problems. In this case, I would break it into (at least) three smaller problems: check for horizonal wins for a given color, then vertical, then diagonal. You only need to check for the specified color, because, if black has just made a move, white could not possibly have a win (or you'd have found it on the previous check). That is, you only need to check for the color that has just placed a piece.

You should be able to crank out the horizontal and vertical checks in a heartbeat, but if you have trouble with the diagonal, report back here. For clarity, I would create at least three different check methods. The check methods should probably return information (coordinates) about where the winning string of 4 starts.

Finally, do you get extra credit if do this thing graphically in Swing, instead of println()? Even if you don't, try to keep your game implementation classes separate from your input and output so that it can be easily adapted to a graphical UI.

Flat View: This topic has 1 reply on 1 page
Topic: EJB ,XML,JSP,Servlets Previous Topic   Next Topic Topic: question on awt

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use