Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Now that we’ve seen an application of a Server/Client program in the real world,

ID: 3576899 • Letter: N

Question

Now that we’ve seen an application of a Server/Client program in the real world, let’s examine another popular industry: Gaming. Games are fun to play with multiple players, and while it’s acceptable to play a game with other people in the room or on a single television, it’s much more fun to play games together remotely – with each player in their own house potentially even across the ocean! To this end, the gaming industry has plenty of uses for Server-based programs like the ones you’ve written all semester, so now we’re going to look at an example and see how it works.

For this project, we’re going to create a popular family-style game called Connect Four. The game is simple to play, with each player taking turns until either player wins. Each player will be running the same Client class, and during RunTime, will be assigned X’s or O’s based on the order in which they connect. The purpose of this assignment is to demonstrate how a Server can work linearly alongside multiple Clients and to reinforce your knowledge of Input and Output between host and clients. The rules of Connect Four are listed on the last page.

Here’s how it works: you’ll start by running your Server class. Once your server is running, it’ll be expecting a connection from exactly two Clients. You’ll run your Client class twice, which will create your two players. Each Client will connect to the server, the first one to connect will be assigned as Player 1 [and either X’s or O’s at your discretion], and the second one to connect will be assigned as Player 2 [and the opposite choice of Player 1]. Once both players are assigned, the Clients will take turns. When their turn starts, the given Client will be shown the current state of the board and be asked for a move to make. You may acquire input from the user in any way you choose, but you must validate entries and prompt for a re-entry if an invalid entry is made. Keep in mind that a chosen column that is full should also result in a re-entry. Once a valid move is entered, the move is made and the next player takes their turn. For simplicity, there will be no need to create an end-game. If you choose to, you may add a condition on which to end the game, but for the purposes of this program it is not necessary so you do not have to check if there are four in a row and prompt a winner.

While you’re writing this program, keep these guidelines in mind:

You must use only one Client class. You cannot run multiple Clients for this project.

Clients must be run individually – you cannot have both Clients in the same output.

Socket, ServerSocket, and all forms of I/O must be closed when your connection is terminated.

Your main method must catch any possible exceptions that may be thrown; it cannot throw an exception as this is bad practice.

Please keep your program code neat and organized. Good use of white space goes a long way.

Do not keep any unused variables or imports around – this is bad practice.

Do not use Threads. There is no need for Threads in this kind of program and they will overcomplicate things.

No GUI applications. Your program must be text-based.

Connect Four Rules

Connect Four is played with two players. In the traditional game, these players would be playing as Red and Black, each with tiles to use matching their chosen color. In this text-based version, I chose to use X’s and O’s.

The board is a simple layout of 7 columns and 6 rows. Each players take turns selecting a column to add a piece to. The piece is added to the bottom-most empty spot of the column, and the next player goes. The first player to line up 4 of their pieces horizontally, vertically, or diagonally is the winner.

For example,

Player 1 is X’s. Player 2 is O’s.

Player 1 starts with the center column. Player 2 then adds a piece to the right. Player 1 adds a piece to the left of their original piece, and Player 2 adds a piece to the center. Given these four moves, the resulting board looks like this:

O

X

X

O

Suppose at this point, the following moves are made:
Player 1 choses Column 3.
Player 2 choses Column 3.
Player 1 choses Column 6.
Player 2 choses Column 2.
Player 1 choses Column 5.
Player 2 choses Column 2.
Player 1 choses Column 4.
Player 2 choses Column 2.
Player 1 choses Column 3.

X

O

O

X

O

X

O

X

O

X

X

O

X

In this instance, Player 1 has won the game by getting four-in-a-row diagonally from column 3 to column 6.

O

X

X

O

Explanation / Answer

Answer:

import java.util.*;

public class TicTacToeGame
{
private int counter;
private char location[]=new char[10];
private char player;
  
  
public static void main(String args[])
{
String ch;
TicTacToeGame Toe=new TicTacToeGame();
do{
Toe.beginBoard();
Toe.play_game();
System.out.println ("Press Y to play the game again ");
Scanner in =new Scanner(System.in);
ch=in.nextLine();
System.out.println("ch value is "+ch);
}while (ch.equals("Y"));
  
  
}
public void beginBoard()
{
  
char locationdef[] = {'0','1', '2', '3', '4', '5', '6', '7', '8', '9'};
int i;
counter = 0;
player = 'X';
for (i=1; i<10; i++) location[i]=locationdef[i];
presentBoard();
  
  
}
public String presentBoard()
{
System.out.println( " " );
System.out.println( " " );
System.out.println( " " + location [1] + " | " +location [2]+ " | " +location [3]);
System.out.println( " | | " );
System.out.println( " ___|____|___ " );
System.out.println( " " +location [4]+ " | " +location [5]+ " | " +location [6]);
System.out.println( " | | " );
System.out.println( " ___|____|___ " );
System.out.println( " " +location [7]+ " | " +location [8]+ " | " +location [9]);
System.out.println( " | | " );
System.out.println( " | | " );
System.out.println( " " );
return "presentBoard";
}
  
public void play_game()
{
int spot;
char blank = ' ';
  
System.out.println( "Player " + getPlayer() +" will go first and be the letter 'X'" );
  
do {
presentBoard();
  
System.out.println( " Player " + getPlayer() +" choose a location." );
  
boolean posTaken = true;
while (posTaken) {
  
Scanner in =new Scanner (System.in);
spot=in.nextInt();
posTaken = checklocation(spot);
if(posTaken==false)
location[spot]=getPlayer();
}
  
System.out.println( "Nice move." );
  
presentBoard();
  
nextPlayer();
}while ( getWinner() == blank );
  
}
  
public char getWinner()
{
char Winner = ' ';
  
  
if (location[1] == 'X' && location[2] == 'X' && location[3] == 'X') Winner = 'X';
if (location[4] == 'X' && location[5] == 'X' && location[6] == 'X') Winner = 'X';
if (location[7] == 'X' && location[8] == 'X' && location[9] == 'X') Winner = 'X';
if (location[1] == 'X' && location[4] == 'X' && location[7] == 'X') Winner = 'X';
if (location[2] == 'X' && location[5] == 'X' && location[8] == 'X') Winner = 'X';
if (location[3] == 'X' && location[6] == 'X' && location[9] == 'X') Winner = 'X';
if (location[1] == 'X' && location[5] == 'X' && location[9] == 'X') Winner = 'X';
if (location[3] == 'X' && location[5] == 'X' && location[7] == 'X') Winner = 'X';
if (Winner == 'X' )
{System.out.println("Player1 wins the game." );
return Winner;
}
  
  
if (location[1] == 'O' && location[2] == 'O' && location[3] == 'O') Winner = 'O';
if (location[4] == 'O' && location[5] == 'O' && location[6] == 'O') Winner = 'O';
if (location[7] == 'O' && location[8] == 'O' && location[9] == 'O') Winner = 'O';
if (location[1] == 'O' && location[4] == 'O' && location[7] == 'O') Winner = 'O';
if (location[2] == 'O' && location[5] == 'O' && location[8] == 'O') Winner = 'O';
if (location[3] == 'O' && location[6] == 'O' && location[9] == 'O') Winner = 'O';
if (location[1] == 'O' && location[5] == 'O' && location[9] == 'O') Winner = 'O';
if (location[3] == 'O' && location[5] == 'O' && location[7] == 'O') Winner = 'O';
if (Winner == 'O' )
{
System.out.println( "Player2 wins the game." );
return Winner; }
  

for(int i=1;i<10;i++)
{
if(location[i]=='X' || location[i]=='O')
{
if(i==9)
{
char Draw='D';
System.out.println(" Game is stalemate ");
return Draw;
}
continue;
}
else
break;
  
}
  
return Winner;
}
  
public boolean checklocation(int spot)
{
  
  
if (location[spot] == 'X' || location[spot] == 'O')
{
System.out.println("That location is already taken, please choose another");
return true;
}
else {
return false;
}
  
  
  
}
  
  
  
public void nextPlayer()
{
if (player == 'X')
player = 'O';
else player = 'X';
  
}
  
public String getTitle()
{
return "Tic Tac Toe" ;
}
  
public char getPlayer()
{
return player;
}
  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote