Write a program to play the traditional version of Connect Four. It should take place on a 6 x 7 game board.
On each turn, the game will print out the state of the board, then alert the current player to make a move. The player's move will consist of a single number, giving the column to place a piece. If the move is invalid, the player will be given the option to select another move.
After each move is made, the game will determine if the current player has won, or if the game was a draw. To simplify the game, we will not check for diagonal wins.
The program should be built using MVC architecture.
Image Credit: Giphy
Use MVC!
import java.lang.IllegalArgumentException;
public class ConnectModel{
private int[][] board;
private int current_player;
public int getCurrentPlayer(){
return this.current_player;
}
public int[][] getBoard(){ return this.board; }
// more code here
}
import java.lang.IllegalArgumentException;
public class ConnectModel{
private int[][] board;
private int current_player;
public int getCurrentPlayer(){
return this.current_player;
}
public int[][] getBoard(){ return this.board; }
// more code here
}
public ConnectModel(int m, int n){
if(m < 4 || n < 5){
throw new IllegalArgumentException("The board
must be at least 4 x 5");
}
this.board = new int[m][n];
this.current_player = 1;
}
public ConnectModel(int[][] board, int current_player){
if(board.length < 4){
throw new IllegalArgumentException("Board ...");
}
if(board[0].length < 5){
throw new IllegalArgumentException("Board ...");
}
if(current_player != 1 && current_player != 2){
throw new IllegalArgumentException("Current ...");
}
this.board = board;
this.current_player = current_player;
}
public boolean makeMove(int y){
}
public boolean makeMove(int y){
if(y < 0 || y >= this.board[0].length){
throw new IllegalArgumentException("Invalid column");
}
}
public boolean makeMove(int y){
if(y < 0 || y >= this.board[0].length){
throw new IllegalArgumentException("Invalid column");
}
if(this.board[0][y] != 0){
throw new IllegalArgumentException("Column full");
}
}
public boolean makeMove(int y){
if(y < 0 || y >= this.board[0].length){
throw new IllegalArgumentException("Invalid column");
}
if(this.board[0][y] != 0){
throw new IllegalArgumentException("Column full");
}
int row = this.board.length - 1;
}
public boolean makeMove(int y){
if(y < 0 || y >= this.board[0].length){
throw new IllegalArgumentException("Invalid column");
}
if(this.board[0][y] != 0){
throw new IllegalArgumentException("Column full");
}
int row = this.board.length - 1;
while(board[row][y] != 0){
row = row - 1;
}
}
public boolean makeMove(int y){
if(y < 0 || y >= this.board[0].length){
throw new IllegalArgumentException("Invalid column");
}
if(this.board[0][y] != 0){
throw new IllegalArgumentException("Column full");
}
int row = this.board.length - 1;
while(board[row][y] != 0){
row = row - 1;
}
this.board[row][y] = this.current_player;
return true;
}
public boolean checkWin(){
}
public boolean checkWin(){
// check rows
for(int i = 0; i < this.board.length; i++){
for(int j = 0; j < this.board[0].length; j++){
}
}
}
public boolean checkWin(){
// check rows
for(int i = 0; i < this.board.length; i++){
int count = 0;
for(int j = 0; j < this.board[0].length; j++){
}
}
}
public boolean checkWin(){
// check rows
for(int i = 0; i < this.board.length; i++){
int count = 0;
for(int j = 0; j < this.board[0].length; j++){
if(this.board[i][j] == this.current_player){
count += 1;
}else{
count = 0;
}
}
}
}
public boolean checkWin(){
// check rows
for(int i = 0; i < this.board.length; i++){
int count = 0;
for(int j = 0; j < this.board[0].length; j++){
if(this.board[i][j] == this.current_player){
count += 1;
}else{
count = 0;
}
if(count >= 4){
return true;
}
}
}
}
public boolean checkWin(){
// check rows
for(int i = 0; i < this.board.length; i++){
int count = 0;
for(int j = 0; j < this.board[0].length; j++){
if(this.board[i][j] == this.current_player){
count += 1;
}else{
count = 0;
}
if(count >= 4){
return true;
}
}
}
// check columns
for(int j = 0; j < this.board[0].length; j++){
int count = 0;
for(int i = 0; i < this.board.length; i++){
if(this.board[i][j] == this.current_player){
count += 1;
}else{
count = 0;
}
if(count >= 4){
return true;
}
}
}
}
public boolean checkWin(){
// check rows
for(int i = 0; i < this.board.length; i++){
int count = 0;
for(int j = 0; j < this.board[0].length; j++){
if(this.board[i][j] == this.current_player){
count += 1;
}else{
count = 0;
}
if(count >= 4){
return true;
}
}
}
// check columns
for(int j = 0; j < this.board[0].length; j++){
int count = 0;
for(int i = 0; i < this.board.length; i++){
if(this.board[i][j] == this.current_player){
count += 1;
}else{
count = 0;
}
if(count >= 4){
return true;
}
}
}
return false;
}
public boolean checkWin(){
// check rows
for(int i = 0; i < this.board.length; i++){
int count = 0;
for(int j = 0; j < this.board[0].length; j++){
if(this.board[i][j] == this.current_player){
count += 1;
}else{
count = 0;
}
if(count >= 4){
return true;
}
}
}
// check columns
for(int j = 0; j < this.board[0].length; j++){
int count = 0;
for(int i = 0; i < this.board.length; i++){
if(this.board[i][j] == this.current_player){
count += 1;
}else{
count = 0;
}
if(count >= 4){
return true;
}
}
}
return false;
}
public boolean checkDraw(){
for(int j = 0; j < this.board[0].length; j++){
if(this.board[0][j] == 0){
return false;
}
}
return true;
}
public void nextPlayer(){
if(this.current_player == 1){
this.current_player = 2;
}else{
this.current_player = 1;
}
}
import java.util.Scanner;
import java.lang.NumberFormatException;
public class ConnectView{
public ConnectView(){
//default constructor
}
// more code here
}
public void showBoard(int[][] board){
for(int i = 0; i < board.length; i++){
for(int j = 0; j < board[0].length; j++){
System.out.print("[" + board[i][j] + "]");
}
System.out.println();
}
}
public void showBoard(int[][] board){
for(int i = 0; i < board.length; i++){
for(int j = 0; j < board[0].length; j++){
System.out.print("[" + board[i][j] + "]");
}
System.out.println();
}
}
[1][2][1][2][1]
[1][2][1][2][1]
[1][2][1][2][1]
[1][2][1][2][1]
[1][2][1][2][1]
public int showMenu(){
while(true){
try{
Scanner reader = new Scanner(System.in);
System.out.println("Which column would you like to place a token in?");
String input = reader.nextLine().trim();
int out = Integer.parseInt(input);
return out;
}catch(NumberFormatException e){
System.out.println("Invalid input! Please enter a number");
}catch(Exception e){
System.out.println("Unable to read input!");
return -1;
}
}
}
public int showMenu(){
while(true){
try{
Scanner reader = new Scanner(System.in);
System.out.println("Which column would you like to place a token in?");
String input = reader.nextLine().trim();
int out = Integer.parseInt(input);
return out;
}catch(NumberFormatException e){
System.out.println("Invalid input! Please enter a number");
}catch(Exception e){
System.out.println("Unable to read input!");
return -1;
}
}
}
public int showMenu(){
while(true){
try{
Scanner reader = new Scanner(System.in);
System.out.println("Which column would you like to place a token in?");
String input = reader.nextLine().trim();
int out = Integer.parseInt(input);
return out;
}catch(NumberFormatException e){
System.out.println("Invalid input! Please enter a number");
}catch(Exception e){
System.out.println("Unable to read input!");
return -1;
}
}
}
public int showMenu(){
while(true){
try{
Scanner reader = new Scanner(System.in);
System.out.println("Which column would you like to place a token in?");
String input = reader.nextLine().trim();
int out = Integer.parseInt(input);
return out;
}catch(NumberFormatException e){
System.out.println("Invalid input! Please enter a number");
}catch(Exception e){
System.out.println("Unable to read input!");
return -1;
}
}
}
public int showMenu(){
while(true){
try{
Scanner reader = new Scanner(System.in);
System.out.println("Which column would you like to place a token in?");
String input = reader.nextLine().trim();
int out = Integer.parseInt(input);
return out;
}catch(NumberFormatException e){
System.out.println("Invalid input! Please enter a number");
}catch(Exception e){
System.out.println("Unable to read input!");
return -1;
}
}
}
public int showMenu(){
while(true){
try{
Scanner reader = new Scanner(System.in);
System.out.println("Which column would you like to place a token in?");
String input = reader.nextLine().trim();
int out = Integer.parseInt(input);
return out;
}catch(NumberFormatException e){
System.out.println("Invalid input! Please enter a number");
}catch(Exception e){
System.out.println("Unable to read input!");
return -1;
}
}
}
public void showTurn(int player){
System.out.println("It is player " + player + "'s turn!");
}
public void showEndGame(int winner){
if(winner == 0){
System.out.println("The game is a draw!");
}else{
System.out.println("Player " + winner + " wins!");
}
}
public void showError(String error){
System.out.println("Error: " + error);
}
public class ConnectController{
public static void main(String[] args){
}
}
public class ConnectController{
public static void main(String[] args){
ConnectModel model = new ConnectModel(6, 7);
ConnectView view = new ConnectView();
}
}
public class ConnectController{
public static void main(String[] args){
ConnectModel model = new ConnectModel(6, 7);
ConnectView view = new ConnectView();
while(true){
}
}
}
public class ConnectController{
public static void main(String[] args){
ConnectModel model = new ConnectModel(6, 7);
ConnectView view = new ConnectView();
while(true){
view.showBoard(model.getBoard());
view.showTurn(model.getCurrentPlayer());
}
}
}
public class ConnectController{
public static void main(String[] args){
ConnectModel model = new ConnectModel(6, 7);
ConnectView view = new ConnectView();
while(true){
view.showBoard(model.getBoard());
view.showTurn(model.getCurrentPlayer());
try{
}catch(Exception e){
view.showError(e.getMessage());
}
}
}
}
public class ConnectController{
public static void main(String[] args){
ConnectModel model = new ConnectModel(6, 7);
ConnectView view = new ConnectView();
while(true){
view.showBoard(model.getBoard());
view.showTurn(model.getCurrentPlayer());
try{
if(model.makeMove(view.showMenu())){
}
}catch(Exception e){
view.showError(e.getMessage());
}
}
}
}
public class ConnectController{
public static void main(String[] args){
ConnectModel model = new ConnectModel(6, 7);
ConnectView view = new ConnectView();
while(true){
view.showBoard(model.getBoard());
view.showTurn(model.getCurrentPlayer());
try{
if(model.makeMove(view.showMenu())){
if(model.checkWin() || model.checkDraw()){
break;
}
model.nextPlayer();
}
}catch(Exception e){
view.showError(e.getMessage());
}
}
}
}
public class ConnectController{
public static void main(String[] args){
ConnectModel model = new ConnectModel(6, 7);
ConnectView view = new ConnectView();
while(true){
view.showBoard(model.getBoard());
view.showTurn(model.getCurrentPlayer());
try{
if(model.makeMove(view.showMenu())){
if(model.checkWin() || model.checkDraw()){
break;
}
model.nextPlayer();
}
}catch(Exception e){
view.showError(e.getMessage());
}
}
if(model.checkWin()){
view.showEndGame(model.getCurrentPlayer());
}else{
view.showEndGame(0);
}
}
}