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

For my java class we have to create a Networked game, and the project must use M

ID: 3858234 • Letter: F

Question

For my java class we have to create a Networked game, and the project must use MVC (Model View Controller) and gameNet to create a networked game.

The classes which gameNet may implement can be found here (LINK FOR GAMENET CLASSES FOUND DIRECTLY BELOW)

https://drive.google.com/open?id=0B8Jh4LT8NcplRW4zcTI4eEI3N0U

Your program must be networked and use "gameNet". I would like to see you use concepts taught in the course wherever possible.

Your program can be a game of your own choosing. I would recommend not taking on more than you can handle. Sample games to consider ( not all equally difficult to implement)

In this project we are trying to simulate a task you might have in industry. Your boss gives you a specification like MVC and some software(like gameNet). Your job is to create some software which works with these specifications and the defined interface.

In the Networked Games Zip file, you will find a package called "gamenet". You will need this for your project. This is the package demo'ed in the MVC gamenet videos. There is also another package called "gamenet_cloneable". In general you don't want to use gamenet_cloneable. It is very similar to gamenet, but there are more requirements for using this package. However, if your game has so much information flying around that gamenet sometimes crashes on you (See the Networked Drawing game where this crash is demo'ed), then you might consider gamenet_cloneable. Discuss the additional ramifications with your teacher if you do need this.

Make sure you understand the MVC (Model View Controller) approach. The following videos go over MVC and how it works with gameNet. Make sure you understand these concepts

MVC, gameNet High Level

gameNet interfaces

Please make a Tic Tac toe game to the specifications stated above:

Explanation / Answer

TTT.java

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tttl;

/**
*
* @author Akshay Bisht
*/
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.Random;

public class TTT extends JFrame implements ChangeListener, ActionListener {
private JSlider slide;
private JButton Obtn, Xbtn;
private GameBoard brd;
private int thick=4;
private Color Ocol=Color.BLUE, XCol=Color.RED;
static final char bl=' ', O='O', X='X';
private char posit[]={
bl, bl, bl,
bl, bl, bl,
bl, bl, bl};
private int win=0, loose=0, tie=0;

public static void main(String args[]) {
new TTT();
}

public TTT() {
super("Tic Tac Toe");
JPanel jp=new JPanel();
jp.setLayout(new FlowLayout());
jp.add(new JLabel("Line Thickness:"));
jp.add(slide=new JSlider(SwingConstants.HORIZONTAL, 1, 20, 4));
slide.setMajorTickSpacing(1);
slide.setPaintTicks(true);
slide.addChangeListener(this);
jp.add(Obtn=new JButton("O Color"));
jp.add(Xbtn=new JButton("X Color"));
Obtn.addActionListener(this);
Xbtn.addActionListener(this);
add(jp, BorderLayout.NORTH);
add(brd=new GameBoard(), BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);
setVisible(true);
}

public void stateChanged(ChangeEvent e) {
thick = slide.getValue();
brd.repaint();
}

public void actionPerformed(ActionEvent e) {
if (e.getSource()==Obtn) {
Color newColor = JColorChooser.showDialog(this, "Choose io new color for O", Ocol);
if (newColor!=null)
Ocol=newColor;
}
else if (e.getSource()==Xbtn) {
Color newColor = JColorChooser.showDialog(this, "Choose io new color for X", XCol);
if (newColor!=null)
XCol=newColor;
}
brd.repaint();
}

private class GameBoard extends JPanel implements MouseListener {
private Random rand=new Random();
private int row[][]={{0,2},{3,5},{6,8},{0,6},{1,7},{2,8},{0,8},{2,6}};

public GameBoard() {
addMouseListener(this);
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
int wid=getWidth();
int ht=getHeight();
Graphics2D graph = (Graphics2D) g;

graph.setPaint(Color.WHITE);
graph.fill(new Rectangle2D.Double(0, 0, wid, ht));
graph.setPaint(Color.BLACK);
graph.setStroke(new BasicStroke(thick));
graph.draw(new Line2D.Double(0, ht/3, wid, ht/3));
graph.draw(new Line2D.Double(0, ht*2/3, wid, ht*2/3));
graph.draw(new Line2D.Double(wid/3, 0, wid/3, ht));
graph.draw(new Line2D.Double(wid*2/3, 0, wid*2/3, ht));

for (int uu=0; uu<9; ++uu) {
double Xposit=(uu%3+0.5)*wid/3.0;
double Yposit=(uu/3+0.5)*ht/3.0;
double radX=wid/8.0;
double radY=ht/8.0;
if (posit[uu]==O) {
graph.setPaint(Ocol);
graph.draw(new Ellipse2D.Double(Xposit-radX, Yposit-radY, radX*2, radY*2));
}
else if (posit[uu]==X) {
graph.setPaint(XCol);
graph.draw(new Line2D.Double(Xposit-radX, Yposit-radY, Xposit+radX, Yposit+radY));
graph.draw(new Line2D.Double(Xposit-radX, Yposit+radY, Xposit+radX, Yposit-radY));
}
}
}

public void mouseClicked(MouseEvent e) {
int Xposit=e.getX()*3/getWidth();
int Yposit=e.getY()*3/getHeight();
int pos=Xposit+3*Yposit;
if (pos>=0 && pos<9 && posit[pos]==bl) {
posit[pos]=O;
repaint();
getX();
repaint();
}
}

public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}

void putX() {
  
if (winner(O))
NG(O);
else if (draw())
NG(bl);

else {
nxtMv();
if (winner(X))
NG(X);
else if (draw())
NG(bl);
}
}

boolean winner(char playa) {
for (int uu=0; uu<8; ++uu)
if (testingR(playa, row[uu][0], row[uu][1]))
return true;
return false;
}

boolean testingR(char playa, int io, int hj) {
return posit[io]==playa && posit[hj]==playa
&& posit[(io+hj)/2]==playa;
}

void nxtMv() {
int bc=searchRow(X);
if (bc<0)
bc=searchRow(O);
if (bc<0) {
do
bc=rand.nextInt(9);
while (posit[bc]!=bl);
}
posit[bc]=X;
}

int searchRow(char playa) {
for (int uu=0; uu<8; ++uu) {
int res=search1Row(playa, row[uu][0], row[uu][1]);
if (res>=0)
return res;
}
return -1;
}

int search1Row(char playa, int io, int hj) {
int c=(io+hj)/2;
if (posit[io]==playa && posit[hj]==playa && posit[c]==bl)
return c;
if (posit[io]==playa && posit[c]==playa && posit[hj]==bl)
return hj;
if (posit[hj]==playa && posit[c]==playa && posit[io]==bl)
return io;
return -1;
}


boolean draw() {
for (int uu=0; uu<9; ++uu)
if (posit[uu]==bl)
return false;
return true;
}

void NG(char winner) {
repaint();

String res;
if (winner==O) {
++win;
res = "You Win!";
}
else if (winner==X) {
++loose;
res = "I Win!";
}
else {
res = "Tie";
++tie;
}
if (JOptionPane.showConfirmDialog(null,
"You have "+win+ " win, "+loose+" loose, "+tie+" tie "
+"Play again?", res, JOptionPane.YES_NO_OPTION)
!=JOptionPane.YES_OPTION) {
System.exit(0);
}

for (int qq=0; qq<9; ++qq)
posit[qq]=bl;

if ((win+loose+tie)%2 == 1)
nxtMv();
}
}
}