Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class assignment
- {
- private static int [][] map = new int[6][6];
- public static void main(String[]args){
- //display maping
- initMap();
- showMap();
- //both player move(including filp & error checking)
- playerMove();
- //counting the Chess of both side
- countChess();
- //tell winner
- gameFinish(int count[]);
- }
- private static void initMap() {
- // inital map
- // loop for y
- for(int i = 0; i < 6; i++) {
- // loop for x
- for (int j = 0; j < 6; j++) {
- // init 1 to map;
- if ((i == 2 && j == 2) || (i == 3 && j == 3)){
- map[i][j] = 1;
- }
- // init 2 to map
- else if ((i == 3 && j == 2) || (i == 2 && j == 3)){
- map[i][j] = 2;
- }
- // init blank space to map
- else {
- map[i][j] = 0;
- }
- }
- }
- }
- public static void showMap() {
- System.out.println(" 0 1 2 3 4 5");
- System.out.println(" -----------");
- for(int i = 0; i < 6; i++){
- System.out.print(i + " | ");
- for(int j = 0; j < 6; j++){
- System.out.print(map[i][j] + " ");
- }
- System.out.println();
- }
- }
- public static void playerMove(){
- Scanner input = new Scanner(System.in);
- int x;
- int y;
- boolean error = true;
- for(int i = 1 , j = 0;j < 32; j++){
- System.out.print("please enter your position '" + i + "':");
- y = input.nextInt();
- x = input.nextInt();
- error = errorChecking(y , x);
- if (error == false){
- continue;
- }
- map[y][x] = i;
- filpChess(y , x , i);
- showMap();
- i = i == 1 ? ++i : --i;
- }
- }
- public static void filpChess(int y ,int x, int player){
- int start_point1 = y;
- int end_point1 = y;
- int start_point2 = x;
- int end_point2 = x;
- //vertical up
- for (int col = y - 1; col >= 0; col--){
- if (map[col][x] == 0){
- // if the grid is empty.
- break;
- }else if (map[col][x] == player){
- start_point1 = col;
- break;
- }
- }
- //vertical down
- for (int col = y + 1; col < 6; col++){
- if (map[col][x] == 0){
- // if the grid is empty0
- break;
- }else if (map[col][x] == player){
- // if found same color chess.
- end_point1 = col;
- break;
- }
- }
- //vertical flip
- for (int col = start_point1; col <= end_point1; col++){
- map[col][x] = player;
- }
- start_point1 = end_point1 = x;
- //horizontal left
- for (int row = x - 1;row >= 0; row--){
- if (map[y][row] == 0){
- // if the grid is empty.
- break;
- }else if (map[y][row] == player){
- start_point1 = row;
- break;
- }
- }
- //horizontal right
- for (int row = x + 1; row < 6; row++){
- if (map[y][row] == 0){
- // if the grid is empty
- break;
- }else if (map[y][row] == player){
- // if found same color chess.
- end_point1 = row;
- break;
- }
- }
- //horizontal flip
- for (int row = start_point1; row <= end_point1; row++){
- map[y][row] = player;
- }
- start_point1 = end_point1 = x;
- start_point2 = end_point2 = y;
- //slash x-- & y--
- for (int col = y - 1 , row = x - 1;col >= 0 && row >= 0; col-- , row--){
- if (map[col][row] == 0){
- // if the grid is empty.
- break;
- }else if (map[col][row] == player){
- start_point1 = row;
- start_point2 = col;
- break;
- }
- }
- //slash x++ & y++
- for (int col = y + 1 , row = x + 1; col < 6 && row < 6; col++ , row++){
- if (map[col][row] == 0){
- // if the grid is empty
- break;
- }else if (map[col][row] == player){
- // if found same color chess.
- end_point1 = col;
- end_point2 = row;
- break;
- }
- }
- for (int col = start_point2 , row = start_point1;col <= end_point2 &&row <= end_point1;col++ , row++){
- map[col][row] = player;
- }
- start_point1 = end_point1 = y;
- start_point2 = end_point2 = x;
- //slash x-- & y++
- for (int col = y + 1, row = x - 1; col < 6 && row >= 0; col++, row--){
- if (map[col][row] == 0){
- // if the grid is empty.
- break;
- }else if (map[col][row] == player){
- start_point1 = col;
- start_point2 = row;
- break;
- }
- }
- //slash x++ & y--
- for (int col = y - 1 , row = x + 1; col >= 0 && row < 6; col--, row++){
- if (map[col][row] == 0){
- // if the grid is empty
- break;
- }else if (map[col][row] == player){
- // if found same color chess.
- end_point1 = col;
- end_point2 = row;
- break;
- }
- }
- for (int col = start_point1, row = start_point2; col >= end_point1 && row <= end_point2; col--, row++){
- map[col][row] = player;
- }
- }
- public static boolean errorChecking(int y ,int x){
- boolean check = true;
- if (x < 0 || x > 5 || y < 0 || y > 5){
- System.out.println("Error - input numbers should be 0 to 5!");
- check = false;
- }else if (map[y][x] > 0){
- System.out.println("Error - input cell is not empty.");
- check = false;
- }
- return check;
- }
- public static int[] countChess(){
- int blackChess = 0;
- int whiteChess = 0;
- int count[] = new int [2];
- for (int col = 0; col < map.length; col++){
- for (int row = 0; row < map[0].length; row++){
- if (map[col][row] == 1){
- blackChess++;
- count[0] = blackChess;
- }
- if (map[col][row] == 2){
- whiteChess++;
- count[1] = whiteChess;
- }
- }
- }
- return count;
- }
- public static int gameFinish(int count[]){
- System.out.println("Game Finish");
- System.out.println("'1' - " + count[0]);
- System.out.println("'2' - " + count[1]);
- if (count[0] > count[1]){
- System.out.println("Black wins");
- }
- else if(count[0] < count[1]){
- System.out.println("white wins");
- }
- else
- System.out.println("That is a draw");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement