Program Connect Four using 2d array in java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ConnectFour {
private static final int boardWidth = 8;
private static final int boardHeight = 8;
int totalMovesPlayed;
int[][] board;
public ConnectFour(){
board = new int[boardHeight][boardWidth];
totalMovesPlayed = 0;
}
public static void main(String args[])throws IOException{
ConnectFour c4 = new ConnectFour();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
outer:
while(true){
int column=0;
while(true){
System.out.println("\n\nPlayer 1 play:");
column=Integer.parseInt(br.readLine());
if(c4.isPlayable(column)){
if(c4.playMove(column, 1)){
c4.printBoard();
System.out.println("\n\nPlayer 1 wins!!!");
break outer;
}
break;
}
else
System.out.println("Column "+column+" is already full!!");
}
c4.printBoard();
while(true){
System.out.println("\n\nPlayer 2 play:");
column=Integer.parseInt(br.readLine());
if(c4.isPlayable(column)){
if(c4.playMove(column, 2)){
c4.printBoard();
System.out.println("\n\nPlayer 2 wins!!!");
break outer;
}
break;
}
else
System.out.println("Column "+column+" is already full!!");
}
c4.printBoard();
if(c4.isFull()){
System.out.print("Game drawn. Both of you suck at this!!! ");
break;
}
}
}
public void printBoard(){
for(int i=0;i<board.length;i++){
for(int j=0;j<board[0].length;j++){
if(board[i][j] == 0)
System.out.print("_ ");
else
System.out.print(board[i][j]+" ");
}
System.out.println();
}
for(int i=0;i<boardWidth;i++)
System.out.print("* ");
System.out.println();
for(int i=0;i<boardWidth;i++)
System.out.print(i+" ");
System.out.println();
}
public boolean playMove(int column, int playerNum){
int i=0;
for(i=0;i<boardHeight;i++){
if(board[i][column] == 1 || board[i][column] == 2){
board[i-1][column]=playerNum;
break;
}
}
if(i == boardHeight)
board[i-1][column]=playerNum;
totalMovesPlayed++;
return isConnected(i-1,column);
}
public boolean isPlayable(int column){
return board[0][column] == 0;
}
public boolean isFull(){
return totalMovesPlayed == boardHeight*boardWidth;
}
public boolean isConnected(int x, int y){
int num=board[x][y];
int count=0;
int i=y;
while(i<boardWidth && board[x][i] == num){
count++;
i++;
}
i=y-1;
while(i>=0 && board[x][i] == num){
count++;
i--;
}
if(count == 4)
return true;
count=0;
int j=x;
while(j<boardHeight && board[j][y] == num){
count++;
j++;
}
if(count == 4)
return true;
count=0;
i=x;
j=y;
while(i<boardWidth && j<boardHeight && board[i][j] == num){
count++;
i++;
j++;
}
i=x-1;
j=y-1;
while(i>=0 && j>=0 && board[i][j] == num){
count++;
i--;
j--;
}
if(count == 4)
return true;
count=0;
i=x;
j=y;
while(i<boardWidth && j>=0 && board[i][j] == num){
count++;
i++;
j--;
}
i=x-1;
j=y+1;
while(i>=0 && j<boardHeight && board[i][j] == num){
count++;
i--;
j++;
}
if(count == 4)
return true;
return false;
}
}
Write a Java program that does the following. a. Declare an integer 2D array with 5 rows and 5 columns. b. Initialize the array's elements to random integers between 1 and 10 (inclusive). c. Display all the elements in the 2D array as a table of rows and columns. d. Display the row index and column index of all the even integers in the 2D array. e. Display the sum of first row's elements.
Create a connect four board game in java. Use multiple methods and 2d arrays. The purpose of the game is to get four red "R" or yellow "Y" pucks in a row(vertically,horizontally, or diagonally)
create a program with files, 1d array, 2d array, and vectors in c++ a single program
In C++ for 2D Array: Write a program that uses a function that manipulates a 2D array of integers as follows: 1. Fill the first row and column in the array with random bits 2. Every subsequent cell should be filled as follows: 1. If the cells that are to the left and top of it are equal, then you should store their sum in it b. Otherwise, your program should store the highest among them. Test your function using...
Java Program: Make an application called that stores at least four different movies, a showtime (such as "11:00am"), and the movie rating (such as "PG-13") in a two-dimensional array. Movies with multiple showtimes should be in the 2d array multiple times, once for each showing. Prompt the user for a movie name and output all showtimes for that film, or a message indicating it was not found.
Using Java, write a program that contains an array. The array will contain the titles of your favorite books, games, shows, or movies. You may make it as large as you desire, but only enter the titles through user input. Sample output: “Dragon Wing”, “Doom Patrol”, “Pokemon”, “Star Wars” NOTE: Include step-by-step instructions in hidden notes.
Write a program which creates a 2D array to store decimal values and passes that array to another function which sets each element of the array to a random value between 0.0 and 10.0.
C++ program 2D array of intergers Initialize the 2-D array of integers detailed below. { 25, 50, 75, 100, 125 }, { 50, 100, 150, 200, 250}, { 75, 150, 225, 300, 375}, { 100, 200, 300, 400, 500}, { 125, 250, 375, 500, 625 }, Traverse and output array elements for the 2D array above in reverse order. Starting with 625, 500, 375, 250, etc…. Attach Snipping Photo of Source Code and Output below. Directly Access and output 225...
java Write methods for 2d 1. a method to calculate the sum of a 2d double array 2. a method to calculate the sum of each row of a 2d double array 3. a method to calculate the sum of each column of a 2d double array 4. a method to calculate the average of a 2d double array 5. a method to calculate the average of each row of a 2d double array 6. a method to calculate the...
Using Java, design a program that creates an array of Card objects, shuffles them, then recursively sorts them by value and then recursively sorts them by value and suit