Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: DOS board game

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default DOS board game

    Hey,

    I'm trying to create a connect four game in Java programming.To give you an image of what it's like, watch this video: YouTube - Connect Four

    Do any of you have an idea on how to create the board and get this all working? Please note that this game is supposed to be played in DOS.

    EDIT: CMD, not DOS!
    Last edited by SageNTitled; March 3rd, 2010 at 04:34 PM. Reason: Wrong word written.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: DOS board game

    DOS? Wow... trip down memory lane

    Anyways, first you will need to decide if you want to a GUI or text input system. If you want a GUI, I would suggest taking a look at Creating a GUI with JFC/Swing. Note there are a ton of components, but I think the ones you will be interested in are JButton, JPanel, JFrame, JLabel, and possibly a few others. To get your GUI to look like a real connect4 board, you can either draw the board manually using different Graphics paint commands, or use the Image class and load a saved image of the board.

    I would also recommend taking a look at Learning the Java Language if you have never programmed in Java before, or have little experience with Java. The first two topics in "learning the basics" should teach you the syntax and terminology to programming in Java.

  3. #3
    Junior Member
    Join Date
    Mar 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: DOS board game

    Wow, I'm sorry. I meant CMD! =(

  4. #4
    Junior Member
    Join Date
    Feb 2010
    Location
    Canada
    Posts
    25
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: DOS board game

    I have a Tic Tac Toe game and a Peg game that runs in the CMD window if you'd like to see my code for that?
    & I also have a simple Tic Tac Toe GUI if you want to see that too?

  5. #5
    Junior Member
    Join Date
    Mar 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: DOS board game

    Yeah, sure! I would be glad!

    Please let me see that code.

  6. #6
    Junior Member
    Join Date
    Feb 2010
    Location
    Canada
    Posts
    25
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: DOS board game

    Just thought Tic Tac Toe and the Peg game are pretty similiar to what you're trying to do. You can use the same idea to pick positions on the board.

    Tic Tac Toe
    -This was before I learned scanner & a lot of other things since it's from my first few weeks of Java.

    import java.io.*;
    import java.util.*;
     
    public class TicTacToe {
     
    public static String GetInput() {
     
    InputStreamReader input = new InputStreamReader(System.in);
    BufferedReader reader = new BufferedReader(input);
     
    String i = "";	
     
    	try {
    	i = reader.readLine();
    	}
    	catch (Exception e) {
    	System.out.println("Error with input");
    	}	
     
    	return i;
     
     
    }	
     
    public static void initialboard(String board[][]) {
    	int c = 0;
    	int e = 0;	
    		//Print out '?' where there are blanks
    		while(c<board.length){
    			while(e<board[c].length){
    				board[c][e]="?";
    				e=e+1;
    			}		
    			c=c+1;
    			e=0;
    		}
    		//hardcode spots on the board where the numbers are(coordinates)
    	board[0][0] = " ";
    	board[0][1] = "1";
    	board[0][2] = "2";
    	board[0][3] = "3";
    	board[1][0] = "1";
    	board[2][0] = "2";
    	board[3][0] = "3";
     
    }
     
    public static void displayboard(String board[][]) {
     
    	int c = 0;
    	int e = 0;
    	//print out every spot in the array
    	while(c<board.length) {
    		while(e<board[c].length) {
    			System.out.print(board[c][e]+" ");
    			e=e+1;
    		}
    	e=0;
    	c=c+1;
    	System.out.print("\n");
    	}
     
     
     
    }
     
    public static boolean checkwinner(String board[][], String p1, String p2) {
     
    		//check every combination for a winner and print out if they win
    	if(board[1][1]=="X"&&board[1][2]=="X"&&board[1][3]=="X") {
    System.out.println(p1 + " WINS !!!");
    return true;		
    }		
    else if(board[1][1]=="O"&&board[1][2]=="O"&&board[1][3]=="O") {
    	System.out.println(p2 + " WINS !!!");
    return true;		
    }
    else if(board[1][1]=="X"&&board[2][1]=="X"&&board[3][1]=="X") {
    	System.out.println(p1 + " WINS !!!");
    return true;
    }	
    else if(board[1][1]=="O"&&board[2][1]=="O"&&board[3][1]=="O") {
    	System.out.println(p2 + " WINS !!!");
    return true;
    }	
    else if(board[2][1]=="X"&&board[2][2]=="X"&&board[2][3]=="X") {
    	System.out.println(p1 + " WINS !!!");
    return true;
    }	
    else if(board[2][1]=="O"&&board[2][2]=="O"&&board[2][3]=="O") {
    	System.out.println(p2 + " WINS !!!");
    return true;
    }
    else if(board[3][1]=="X"&&board[3][2]=="X"&&board[3][3]=="X") {
    	System.out.println(p1 + " WINS !!!");
    return true;
    }	
    else if(board[3][1]=="O"&&board[3][2]=="O"&&board[3][3]=="O") {
    	System.out.println(p2 + " WINS !!!");
    return true;
    }
    else if(board[1][2]=="X"&&board[2][2]=="X"&&board[3][2]=="X") {
    	System.out.println(p1 + " WINS !!!");
    return true;
    }
    else if(board[1][2]=="O"&&board[2][2]=="O"&&board[3][2]=="O") {
    	System.out.println(p2 + " WINS !!!");
    return true;
    }
    else if(board[1][3]=="X"&&board[2][3]=="X"&&board[3][3]=="X") {
    	System.out.println(p1 + " WINS !!!");
    return true;
    }	
    else if(board[1][3]=="O"&&board[2][3]=="O"&&board[3][3]=="O") {
    	System.out.println(p2 + " WINS !!!");
    return true;
    }
    else if(board[1][1]=="X"&&board[2][2]=="X"&&board[3][3]=="X") {
    	System.out.println(p1 + " WINS !!!");
    return true;
    }
    else if(board[1][1]=="O"&&board[2][2]=="O"&&board[3][3]=="O") {
    	System.out.println(p2 + " WINS !!!");
    return true;
    }
    else if(board[1][3]=="X"&&board[2][2]=="X"&&board[3][1]=="X") {
    	System.out.println(p1 + " WINS !!!");
    return true;
    }
    else if(board[1][3]=="O"&&board[2][2]=="O"&&board[3][1]=="O") {
    	System.out.println(p2 + " WINS !!!");
    return true;
    }
    else {
    	return false;
    }
     
    }
     
    public static boolean checkfull(String board[][]) {
     
     	int c1 = 1;
    	int c2 = 1;	
    //check if there are any available spots on the board
    while(c1<board.length) {
    	while(c2<board[c1].length) {
    		if(board[c1][c2].equals("?")) {
    			return false;
    		}
    		c2=c2+1;
    	}
    	c2=0;
    	c1=c1+1;
    }
    return true;	
    }
     
    public static void main(String args[]) {
     
    // declare variables
    boolean winner = false;
    String choice = "Yes";
    String board[][] = new String[4][4];
    String temp = " ";
    int row = 0;
    int column = 0;
    String p1 = " ";
    String p2 = " ";
    String letter = "X";
    String player = " ";
     
    // play again loop
    while (choice.compareToIgnoreCase("No")!=0&&choice.compareToIgnoreCase("N")!=0) {
     
    // re-declare variables
    winner = false;
    row = 0;
    column = 0;
    letter = "X";
    temp = " ";
    initialboard(board);
    player = " ";
    System.out.println("Player 1 enter your name");
    p1 = GetInput();
    System.out.println("Player 2 enter your name");
    p2 = GetInput();
    // instrutions
    System.out.println("Tic Tac Toe. Enter the row, then enter the column.");
     
    // game loop
    while(!winner) {
     
    //change the board everytime a spot is changed
    displayboard(board);
     
    // Switch between player names depending on X or O
    if(letter=="X") {
    	player = p1;
    }
    else {
    	player = p2;
    }
    // get input from user
    	System.out.println(player + " enter the row");
    temp = GetInput();
    row = Integer.parseInt(temp);
    System.out.println(player + " enter the column");
    temp = GetInput();
    column = Integer.parseInt(temp);
     
    // Switch between players and dont allow them to pick the same spot twice
    if(board[row][column].equals("?")) {
    board[row][column] = letter;
    //check for winner
    winner = checkwinner(board,p1,p2);
    //check if board is full
    if(checkfull(board)&&!winner){
    	System.out.println("Tie game");
    	break;	
    }
    if(letter.equals("X")) {
    	letter = "O";
    }
    else {	
    	letter = "X";	
    }
    }
    else {
    	System.out.println("Spot taken");
    }
    } 			
    			System.out.println("Would you like to play again?");
     
    				choice = GetInput();
     
    			if(choice.compareToIgnoreCase("Yes")==0) {
    				System.out.println("Yes was chosen");
    			}
    			else if(choice.compareToIgnoreCase("Y")==0) {
    				System.out.println("Yes was chosen");
    			}
    			else if(choice.compareToIgnoreCase("N")==0) {
    				System.out.println("No was chosen");
    			}
    			else if(choice.compareToIgnoreCase("No")==0) {
    				System.out.println("No was chosen");
    			}
    			else {
    				System.out.println("Invalid choice");
    			}								
    		} 		
    }
    }


    Peg Game
    -I'm sure almost everyone has played this game, but if you haven't, look up the rules

    import java.io.*;
    import java.util.*;
     
    public class PegGame {
     
    public static String GetInput() {
     
    InputStreamReader input = new InputStreamReader(System.in);
    BufferedReader reader = new BufferedReader(input);
     
    String i = "";	
     
    	try {
    	i = reader.readLine();
    	}
    	catch (Exception e) {
    	System.out.println("Error with input");
    	}	
     
    	return i;	
    }		
     
    public static void initialboard(String board[][]) {
    	int c = 0;
    	int e = 0;	
    		//prints out 'O' in any spot that isn't taken
    		while(c<board.length){
    			while(e<board[c].length){
    				board[c][e]="O";
    				e=e+1;
    			}		
    			c=c+1;
    			e=0;
    		}
     
    	//numbers	
    	board[0][1] = "1";
    	board[0][2] = "2";
    	board[0][3] = "3";
    	board[0][4] = "4";
    	board[0][5] = "5";
    	board[0][6] = "6";
    	board[0][7] = "7";	
    	board[1][0] = "1";
    	board[2][0] = "2";
    	board[3][0] = "3";
    	board[4][0] = "4";
    	board[5][0] = "5";
    	board[6][0] = "6";	
    	board[7][0] = "7";	
    	//empty spots
    	board[0][0] = " ";
    	board[1][1] = "-";	
    	board[1][2] = "-";
    	board[2][1] = "-";
    	board[2][2] = "-";
    	board[1][6] = "-";
    	board[1][7] = "-";
    	board[2][6] = "-";
    	board[2][7] = "-";
    	board[6][1] = "-";
    	board[6][2] = "-";
    	board[7][1] = "-";
    	board[7][2] = "-";
    	board[6][6] = "-";
    	board[6][7] = "-";
    	board[7][6] = "-";
    	board[7][7] = "-";
    	board[4][4] = " ";
    }
     
    public static void displayboard(String board[][]) {
     
    	//prints out all the spots in the array
    	int c = 0;
    	int e = 0;
     
    	while(c<board.length) {
    		while(e<board[c].length) {
    			System.out.print(board[c][e]+" ");
    			e=e+1;
    		}
    	e=0;
    	c=c+1;
    	System.out.print("\n");
    	}
     
     
     
    }
     
    public static boolean checkmoves(String board[][]) {
    //row = r, column = c
    int r = 1;
    int c = 1;	
    //if there are no moves, it returns true
     
    	//checks right and down
    	while(r<board.length-2){
    		while(c<board.length-2) {
    			if(board[r][c].equals("O")&&board[r+1][c].equals("O")&&board[r+2][c].equals(" ")) {				
     
    				return true;
    			}
    			if(board[r][c].equals("O")&&board[r][c+1].equals("O")&&board[r][c+2].equals(" ")) {
     
    				return true;
    			}
    			c=c+1;
    		}
    		c=0;
    		r=r+1;
    	}	
    	r=board.length-1;
    	c=board.length-1;
    	//checks left and up
    	while(r>2){
    		while(c>2) {
    			if(board[r][c].equals("O")&&board[r-1][c].equals("O")&&board[r-2][c].equals(" ")) {			
    				return true;
    			}
    			if(board[r][c].equals("O")&&board[r][c-1].equals("O")&&board[r][c-2].equals(" ")) {
    				return true;
    			}
    			c=c-1;
    		}
    		c=board.length-1;
    		r=r-1;
    	}
    	//return false if there is at least one move left
    	return false;	
    }
     
    public static int checkpegs(String board[][], int pegs) {
     
    //row = r, column = c	
    int r = 1;
    int c = 1;	
    	//checks the entire array for any 'O'
    	while(r<board.length){
    		while(c<board.length) {
    			if(board[r][c].equals("O")) {
    					pegs = pegs + 1;				
    				}
     
    			c=c+1;
    		}
    		c=0;
    		r=r+1;
    	}	
    	return pegs;
    	//looks for all the remaining pegs in the board and returns it
     
    }
     
    public static void main(String args[]) {
     
    //declare variables
    String board[][] = new String[8][8];	
    String choice = "Yes";
    boolean winner = false;
    int sr = 0;
    int sc = 0;
    int er = 0;
    int ec = 0;
    int score = 10;
    int pegs = 0;
     
    //play again loop
    while (choice.compareToIgnoreCase("No")!=0&&choice.compareToIgnoreCase("N")!=0) {
    //re-declare variables
    winner = false;
    pegs = 0;
    score = 10;
    System.out.println("Peg Game. 'O' are pegs and '-' are unusable");
    System.out.println("Every peg leftover at the end of the game is -1 point from 10.");
    System.out.println();
    initialboard(board);
     
    //game loop
    while(!winner) {
     
    //re-displays board everytime there is input
    displayboard(board);
     
    //calls checkmoves function to see if there's any moves left
    if(!checkmoves(board)) {
    	pegs = checkpegs(board, pegs);	
    	score = score - pegs;
    	System.out.println("No more moves, your score is " + score);
    	//call checkpegs function when no more moves to count the remaining pegs	
    	break;
    }
     
    // get input and change it into int's so you can put it into the board array
    // ask for a start and end point
    	System.out.print("Enter the start row ");	
    	sr = Integer.parseInt(GetInput());
    	System.out.print("Enter the start column ");	
    	sc = Integer.parseInt(GetInput());
    	System.out.print("Enter the end row ");
    	er = Integer.parseInt(GetInput());
    	System.out.print("Enter the end column ");
    	ec = Integer.parseInt(GetInput());
     
    // left right movements	
    if(sr==er&&!board[sr][sc].equals("-")&&!board[er][ec].equals("-")) {
    	if(sc>ec) {
    		if(board[er][ec].equals(" ")&&board[sr][sc].equals("O")&&board[sr][sc-1].equals("O")) {
    			//left
    			board[er][ec] = "O";
    			board[sr][sc] = " ";
    			board[sr][sc-1] = " ";
    		}
    		else {
    			System.out.println("Invalid move");
    		}
    	}
    	else{
    		//right
    		board[er][ec] = "O";
    		board[sr][sc] = " ";
    		board[sr][sc+1] = " ";
     
    	}	
    }
     
    // up down movements
    else if(sc==ec&&!board[sr][sc].equals("-")&&!board[er][ec].equals("-")) {
    	if(sr>er) {
    		if(board[er][ec].equals(" ")&&board[sr][sc].equals("O")&&board[sr-1][sc].equals("O")) {
    			//down			
    			board[er][ec] = "O";
    			board[sr][sc] = " ";
    			board[sr-1][sc] = " ";
    		}
    		else {
    			System.out.println("Invalid move");
    		}
    	}
    	else {
    		//up
    		board[er][ec] = "O";
    		board[sr][sc] = " ";
    		board[sr+1][sc] = " ";
    	}
    }	
     
     
    }
     
    	System.out.println("Would you like to play again?");
     
    				choice = GetInput();
     
    			if(choice.compareToIgnoreCase("Yes")==0) {
    				System.out.println("Yes was chosen");
    			}
    			else if(choice.compareToIgnoreCase("Y")==0) {
    				System.out.println("Yes was chosen");
    			}
    			else if(choice.compareToIgnoreCase("N")==0) {
    				System.out.println("No was chosen");
    			}
    			else if(choice.compareToIgnoreCase("No")==0) {
    				System.out.println("No was chosen");
    			}
    			else {
    				System.out.println("Invalid choice");
    			}								
    		} 		
    }
    }


    Ask about anything in the code you want and good luck!

Similar Threads

  1. Game 3x3
    By Koren3 in forum Algorithms & Recursion
    Replies: 1
    Last Post: December 20th, 2009, 08:43 PM
  2. Breakout Game
    By Ceasar in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 9th, 2009, 01:30 AM
  3. invisible box game
    By new2java in forum Loops & Control Statements
    Replies: 1
    Last Post: September 27th, 2009, 01:46 PM
  4. Job offers to program Hobo Wars
    By MooncakeZ in forum Paid Java Projects
    Replies: 7
    Last Post: September 17th, 2009, 10:41 PM
  5. [SOLVED] Fixing of bug for Pong game
    By phoenix in forum What's Wrong With My Code?
    Replies: 11
    Last Post: July 14th, 2009, 02:19 PM