Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class ct4 {
- static char[][]board = new char [6][7];
- static Scanner kb = new Scanner(System.in);
- static int player = 0;
- static int redWins =0, yellowWins=0;
- static int r = 0,c = 0;
- static char color;
- static int totalMoves = 42;
- static boolean result1, result2;
- public static void initboard(char[][] b){
- for (int r=0; r<b.length; r++) {
- for (int c=0; c<b[0].length; c++) {
- b[r][c]= ' ';
- }
- }
- }
- public static char[][] displayBoard(char[][] b){
- System.out.println();
- for (int r = 0; r< b.length; r++){
- System.out.print(" |");
- for (int c = 0; c< b[0].length; c++){
- System.out.print(" " + b[r][c] + " |");
- }
- System.out.println();
- }
- System.out.println(" -----------------------------");
- System.out.println(" | 1 | 2 | 3 | 4 | 5 | 6 | 7 |");
- return board;
- }
- public static void move(int c) {
- Scanner kb= new Scanner(System.in);
- if(player%2==0)
- {
- System.out.println("Player 1");
- System.out.println("Which column do you want to move?");
- do{
- c = kb.nextInt();
- if(c<0 || c>7){
- System.out.println("Invalid, choose another column");
- }
- }while(c<0 || c>7);
- c--;
- for(int r=5;r>-1;r--){
- if(board[r][c] == ' '){
- board[r][c]= color;
- break;
- }
- else if(board[0][c]!=' '){
- System.out.println("Invalid, Column full choose another column");
- do{
- c = kb.nextInt();
- if(c<0 || c>7){
- System.out.println("Invalid, choose another column");
- }
- board[r][c-1]=color;
- }while(c<0 || c>7);
- break;
- }
- }
- }
- else{
- System.out.println("Player 2");
- System.out.println("Which column do you want to move?");
- do{
- c = kb.nextInt();
- if(c<0 || c>7){
- System.out.println("Invalid, choose another column");
- }
- }while(c<0 || c>7);
- c--;
- for(int r=5;r>-1;r--){
- if(board[r][c] == ' '){
- board[r][c]= color;
- break;
- }
- else if(board[0][c]!=' '){
- System.out.println("Invalid, Column full choose another column");
- do{
- c = kb.nextInt();
- if(c<0 || c>7){
- System.out.println("Invalid, choose another column");
- }
- board[r][c-1]= color;
- }while(c<0 || c>7);
- break;
- }
- }
- }
- player++;
- }
- public static void whichPlayer () {
- if(player%2==0) {
- System.out.println("Player 1's Turn");
- color ='Y';
- }
- else {
- System.out.println("Player 2's Turn");
- color ='R';
- }
- }
- public static boolean checkRedWin(char[][]b, int r, int c, char R) {
- if((r+3)< b[0].length){
- if((b[r][c]=='R')&&(b[r][c+1]=='R')&&(b[r][c+2]=='R')&&(b[r][c+3]=='R')) {
- result1 = true;
- redWins++;
- System.out.println("Red is the winner!");
- return true;
- }
- if((r+3)<b.length){
- if((b[r][c]=='R')&&(b[r+1][c]=='R')&&(b[r+2][c]=='R')&&(b[r+3][c]=='R')) {
- result1 = true;
- redWins++;
- System.out.println("Red is the winner!");
- return true;
- }
- if ((r+3)<b.length &&((c+3)<b[0].length)){
- if((b[r][c]=='R')&&(b[r+1][c+1]=='R')&&(b[r+2][c+2]=='R')&&(b[r+3][c+3]=='R')) {
- result1 = true;
- redWins++;
- System.out.println("Red is the winner!");
- return true;
- }
- }
- if ((r+3)>0&&((c+3)<b[0].length)){
- if((b[r][c]=='R')&&(b[r-1][c+1]=='R')&&(b[r-2][c+2]=='R')&&(b[r-3][c+3]=='R')) {
- result1 = true;
- redWins++;
- System.out.println("Red is the winner!");
- return true;
- }
- }
- }
- }
- return false;
- }
- public static boolean checkYellowWin(char[][]b, int r, int c, char Y) {
- if((r+3)< b[0].length){
- if((b[r][c]=='Y')&&(b[r][c+1]=='Y')&&(b[r][c+2]=='Y')&&(b[r][c+3]=='Y')) {
- result2 = true;
- yellowWins++;
- System.out.println("Yellow is the winner!");
- return true;
- }
- if((r+3)<b.length){
- if((b[r][c]=='Y')&&(b[r+1][c]=='Y')&&(b[r+2][c]=='Y')&&(b[r+3][c]=='Y')) {
- result2 = true;
- yellowWins++;
- System.out.println("Yellow is the winner!");
- return true;
- }
- if ((r+3)<b.length &&((c+3)<b[0].length)){
- if((b[r][c]=='Y')&&(b[r+1][c+1]=='Y')&&(b[r+2][c+2]=='Y')&&(b[r+3][c+3]=='Y')) {
- result2 = true;
- yellowWins++;
- System.out.println("Yellow is the winner!");
- return true;
- }
- }
- if ((r+3)>0&&((c+3)<b[0].length)){
- if((b[r][c]=='Y')&&(b[r-1][c+1]=='Y')&&(b[r-2][c+2]=='Y')&&(b[r-3][c+3]=='Y')) {
- result2 = true;
- yellowWins++;
- System.out.println("Yellow is the winner!");
- return true;
- }
- }
- }
- }
- return false;
- }
- public static boolean checkWinner(char[][]b) {
- boolean red, yellow;
- for (int r=0; r<b.length; r++) {
- for (int c=0; c<b[0].length;c++) {
- red = checkRedWin(displayBoard(b),r,c,'R');
- yellow = checkYellowWin(displayBoard(b),r,c,'Y');
- if(red==true || yellow==true) {
- return true;
- }
- }
- }
- return false;
- }
- public static void checkdraw(char[][]b){
- for(int r=0; r<b.length; r++){
- for(int c=0;c<b[0].length;c++){
- if(b[r][c]!=' '){
- System.out.println("The game is a draw!");
- break;
- }
- }
- }
- }
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- System.out.println("Player 1 is: Y = Yellow");
- System.out.println("Player 2 is: R = Red ");
- int c=0;
- int redWins=0, yellowWins=0;
- int playAgain = 0;
- int moves = 0;
- do {
- initboard(board);
- displayBoard(board);
- result1=false;
- result2=false;
- player=0;
- do {
- whichPlayer();
- move(c);
- checkWinner(board);
- moves++;
- if(totalMoves==moves){
- checkdraw(board);
- break;
- }
- } while(result1!= true && result2!= true);
- System.out.println("Game Over!");
- System.out.println("Do you want to play again? 1 for Yes, 0 for No");
- playAgain = kb.nextInt();
- if(result1==true){
- redWins++;
- }
- System.out.println("Red wins: " + redWins);
- if(result2==true){
- yellowWins++;
- }
- System.out.println("Yellow wins: " + yellowWins);
- } while(playAgain==1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement