Java程序连接四个游戏

时间:2015-11-12 23:38:30

标签: java methods

所以除了一部分之外,这个程序运作得很好。当我把检查器放下时。比方说我将红色放入1,然后尝试将蓝色放入1,它应该叠加在红色的顶部,但它取而代之的是红色的红色。我将发布整个代码。一切都有帮助!

package com.company; import java.util.Scanner;

public class ConnectFour {
    public static String[][] makeboard() {
        String[][] CJ = new String[7][15];

        for (int i = 0; i < CJ.length; i++) {

            for (int c = 0; c < CJ[i].length; c++) {
                if (c % 2 == 0)
                    CJ[i][c] = "|";
                else CJ[i][c] = " ";
                if (i == 6) CJ[i][c] = "-";
            }
        }
        return CJ;

    }

    public static void printboard(String[][] CJ) {

        for (int i = 0; i < CJ.length; i++) {
            for (int c = 0; c < CJ[i].length; c++) {
                System.out.print(CJ[i][c]);
            }
            System.out.println();
        }
    }

//此部分不起作用

    public static void Red(String[][] CJ,String player1) {

        System.out.println(player1+ " enter 0-6 to place checker");
        Scanner scnr = new Scanner(System.in);

        int g = (2 * (scnr.nextInt()) + 1);
        for (int i = 5; i >= 0; i--) {
            if (CJ[i][g] == " ") ;
            {
                CJ[i][g] = "R";
                break;
            }
        }
    }

//此部分不起作用

    public static void Blue(String[][] CJ, String player2) {

        System.out.println(player2 +" enter 0-6 to place checker");
        Scanner scnr = new Scanner(System.in);

        int g = (2 * (scnr.nextInt()) + 1);
        for (int i = 5; i >= 0; i--) {
            if (CJ[i][g] == " ") ;
            {
                CJ[i][g] = "B";
                break;

            }
        }
    }

//

    public static String checkforWinner(String[][]CJ){


        for( int i=0; i<6; i++){
            for(int j=0; j<7; j++){
                if      ((CJ[i][j+1] != " ")
                        && (CJ[i][j+3] != " ")
                        && (CJ[i][j+5] != " ")
                        && (CJ[i][j+7] != " ")
                        && ((CJ[i][j+1] == CJ[i][j+3])
                        && (CJ[i][j+3] == CJ[i][j+5])
                        && (CJ[i][j+5] == CJ[i][j+7])))
                    return CJ[i][j+1];
            }
        }
        for (int j=1; j<15; j+=2){


            for (int i =0; i<3; i++){
                if      ((CJ[i][j] != " ")
                        && (CJ[i+1][j] != " ")
                        && (CJ[i+2][j] != " ")
                        && (CJ[i+3][j] != " ")
                        && ((CJ[i][j] == CJ[i+1][j])
                        && (CJ[i+1][j] == CJ[i+2][j])
                        && (CJ[i+2][j] == CJ[i+3][j])))
                    return CJ[i][j];


            }
        }
        for (int i=0;i<3;i++){

            for (int k=7; k<15; k+=2){
                if ((CJ[i][k] != " ")
                        &&(CJ[i+1][k-2] != " ")
                        &&(CJ[i+2][k-4] != " ")
                        &&(CJ[i+3][k-6] != " ")
                        &&((CJ[i][k] == CJ[i+1][k-2])
                        &&(CJ[i+1][k-2]==CJ[i+2][k-4])
                        &&(CJ[i+2][k-4]==CJ[i+3][k-6])))
                    return CJ[i][k];
            }
        }
        return null;
    }
    public static void main(String[] args){
        Scanner scnr = new Scanner(System.in);
        System.out.println("Player one enter your name.");
        String player1= scnr.nextLine();
        System.out.println("Player two enter your name.");
        String player2 = scnr.nextLine();
        System.out.println(player1 + " is Red and " + player2 + " is Blue");
        String[][] CJ= makeboard();
        boolean playing = true;
        int count = 0;
        printboard(CJ);
        while(playing){
            if(count %2==0) Red(CJ,player1);
            else  Blue(CJ,player2);
            count++;
            printboard(CJ);}
            if (checkforWinner(CJ) != null){
                if (checkforWinner(CJ) =="R")
                    System.out.println(player1 + " wins!");
                else if (checkforWinner(CJ) == "B")
                    System.out.println(player2 + " wins!");

            }
        }


    }

0 个答案:

没有答案