Jump to content

Hey guys, i have yet another coding question for you guys. i just hope that i can write it out and explain what i want to do properly. So what i want to do is create a game of tic tac toe using very basic java (it cant be too fancy as it can be at the level of what we have learnt in class) it needs to have a 2d array and be an applet. i think i can handle everything but the 2d array has got me stumped it needs to be a block of 9 individual boxes (obviously) and each one of those boxes is going to be 100x100pixels. i cant paint that out using paint() but i cannot get the array to match it.. if that makes sense.. this is what i have got so far. (sorry wall of text incoming)

import java.awt.*;import java.applet.Applet;import java.awt.event.*;@SuppressWarnings("serial")public class NoughtsCrosses extends Applet implements MouseListener { 		int mouseX,mouseY,mapFactor;	int[][] board = new int[3][3];		public void init(){		this.setSize(600,600);		addMouseListener(this);	board[2][2] = 1; 	mapFactor = 100;	}		public void paint (Graphics g){				for (int row=0; row<=2; row++)		{			for (int colum=0; colum<=2; colum++)			{				g.drawRect(colum*100, row*100, 100, 100);						}		}		g.drawString(""+mouseX+"  "+mouseY,10,10);			}		public void mouseClicked (MouseEvent e){		mouseX = e.getX();		mouseY = e.getY();		repaint();	}			public void mouseEntered(MouseEvent e){		}	public void mouseExited(MouseEvent e){		}	public void mousePressed(MouseEvent e) {				}		public void mouseReleased(MouseEvent e) {				}	}

YES ITS A MESS I KNOW DON'T HURT ME

SYSTEM SPECS

CPU> Intel 4790k< GPU> EVGA GTX970< SSD> Crucial MX200 250Gb< HDD> Seagate Barracuda 2Tb<
Cooling> Corsair H100i< Case> Corsair Air 540< PSU> Seasonic X-Series 650W< RAM> 8Gb Kingston HyperX<
Link to comment
https://linustechtips.com/topic/171945-noughts-and-crosses-java-help/
Share on other sites

Link to post
Share on other sites

Why not drop the array completely? Now I'm going to psudo code because I'm on my mobile.

 

Instead you could make an abstract class... call it entity.

abstract class Entity{  int X { get; set; }  int Y { get; set; }  int Width { get; set; } // This will have the same method of calculation  int Height { get; set; } // This will have the same method of calculation  void Render(Graphics g);}

Entity is implemented by Circle or Cross or Square:

class Cross : Entity{  // Implementation of specific "X" rendering}class Circle : Entity{  // Implementation of specific "O" rendering}class Square : Entity{  // You get the idea now...}

You then store these in a generic list: List<Entity>... Handlers check X, Y, Width and Height for each entity and so on and so fourth.

 

You might be fine with just one Entity class which has a render method for each type of thing instead and changes based on logic... depends really.

The single biggest problem in communication is the illusion that it has taken place.

Link to post
Share on other sites

I actually did something similar to this once in an applet Matching Squares game. Had an server based score chart component, difficulty ramping, images and sounds. Was fun, those were the days and I envy you them!

 

No I'm not going to suggest that you use a Quadtree to implement this.... before they come mocking.

The single biggest problem in communication is the illusion that it has taken place.

Link to post
Share on other sites

Haha thanks for the help mate, but  to be honest i am only about 6 months into the first year of this course and that made pretty much no sense to me at all :P
im sure i will think of something :P

SYSTEM SPECS

CPU> Intel 4790k< GPU> EVGA GTX970< SSD> Crucial MX200 250Gb< HDD> Seagate Barracuda 2Tb<
Cooling> Corsair H100i< Case> Corsair Air 540< PSU> Seasonic X-Series 650W< RAM> 8Gb Kingston HyperX<
Link to post
Share on other sites

Haha thanks for the help mate, but  to be honest i am only about 6 months into the first year of this course and that made pretty much no sense to me at all :P

im sure i will think of something :P

 

I'm sorry, I'll see if I get some time to throw something together really quick for you.

 

6 months in and they still haven't taught you about splitting your code into classes?

The single biggest problem in communication is the illusion that it has taken place.

Link to post
Share on other sites

Here you go; this is very rushed and rough, done in the presence of my two screaming children. You might want to learn about double buffering as well - it will eliminate the flicker and make it more responsive.

import java.awt.*;import java.applet.Applet;import java.awt.event.*;import java.util.List;import java.util.ArrayList;;@SuppressWarnings("serial")public class NoughtsCrosses extends Applet implements MouseListener {	private static final int Width = 100;	private static final int Height = 100;	private List<Entity> _entities;		public void init()	{		setSize(600, 600);		_entities = new ArrayList<Entity>();				int xOffset = 0;		int yOffset = 0;		int rowMaker = 0;		int rowCount = 0;				for (int i = 0; i <= 8; i++)		{			xOffset = Width * rowMaker;						if (rowMaker == 3)			{				rowCount++;				rowMaker = 0;				xOffset = 0;				yOffset = Height * rowCount;			}						_entities.add(new Entity(xOffset, yOffset, Width, Height));			rowMaker++;		}				addMouseListener(this);	}		public void paint (Graphics g)	{		for (Entity e : _entities)		{			e.render(g);		}	}		public void mouseClicked (MouseEvent e)	{		for (Entity en : _entities)		{			if (en.amIHit(e.getX(), e.getY()))			{				if (en.getMode() == Mode.Empty)				{					en.setMode(Mode.Cross);				}				else if (en.getMode() == Mode.Cross)				{					en.setMode(Mode.Nought);				}				else				{					en.setMode(Mode.Empty);				}				break;			}		}				repaint();	}			public void mouseEntered(MouseEvent e) { }		public void mouseExited(MouseEvent e) { }		public void mousePressed(MouseEvent e) { }		public void mouseReleased(MouseEvent e) { }}
import java.awt.*;public class Entity {	private Rectangle _rect;	private Mode _mode;		public Entity(int x, int y, int width, int height)	{		_mode = Mode.Empty;		_rect = new Rectangle(x, y, width, height);	}		public Rectangle getRectangle()	{		return _rect;	}		public boolean amIHit(int x, int y)	{		return _rect.contains(x, y);	}		public Mode getMode()	{		return _mode;	}		public void setMode(Mode m)	{		_mode = m;	}		public void render(Graphics g)	{		if (getMode() == Mode.Cross)			renderCross(g);		else if (getMode() == Mode.Nought)			enderNought(g);		else			renderEmpty(g);	}		private void renderEmpty(Graphics g)	{		g.drawRect(_rect.x,  _rect.y, _rect.width, _rect.height);	}		private void renderCross(Graphics g)	{		renderEmpty(g);		g.drawLine(_rect.x, _rect.y, _rect.x + _rect.width, _rect.y + _rect.height);		g.drawLine(_rect.x + _rect.width, _rect.y, _rect.x, _rect.y + _rect.height);	}		private void enderNought(Graphics g)	{		renderEmpty(g);		g.drawOval(_rect.x, _rect.y, _rect.width, _rect.height);	}}
public enum Mode {	Empty,	Cross,	Nought}

The single biggest problem in communication is the illusion that it has taken place.

Link to post
Share on other sites

Quick and dirty double buffering example, compare the two at runtime and hopefully you can see the difference. One would normally push the double buffering logic into a base class deriving from Applet and then derive again for actual implementation. I have left it all in the same class so you can get an idea of what's going on.

import java.awt.*;import java.applet.Applet;import java.awt.event.*;import java.util.List;import java.util.ArrayList;;@SuppressWarnings("serial")public class NoughtsCrosses extends Applet implements MouseListener {	private static final int AppletHeight = 600;	private static final int AppletWidth = 600;	private static final int Width = 100;	private static final int Height = 100;	private List<Entity> _entities;	private Graphics _buffer;	private Image _offscreen;		public void init()	{		setSize(AppletWidth, AppletHeight);		_offscreen = createImage(AppletWidth, AppletHeight);		_buffer = _offscreen.getGraphics();				_entities = new ArrayList<Entity>();				int xOffset = 0;		int yOffset = 0;		int rowMaker = 0;		int rowCount = 0;				for (int i = 0; i <= 8; i++)		{			xOffset = Width * rowMaker;						if (rowMaker == 3)			{				rowCount++;				rowMaker = 0;				xOffset = 0;				yOffset = Height * rowCount;			}						_entities.add(new Entity(xOffset, yOffset, Width, Height));			rowMaker++;		}				addMouseListener(this);	}		public void paint (Graphics g)	{		_buffer.clearRect(0, 0, AppletWidth, AppletHeight); 				for (Entity e : _entities)		{			e.render(_buffer);		}				g.drawImage(_offscreen, 0, 0, this);	}		public void update(Graphics g) 	{ 	     paint(g); 	} 		public void mouseClicked (MouseEvent e)	{		for (Entity en : _entities)		{			if (en.amIHit(e.getX(), e.getY()))			{				if (en.getMode() == Mode.Empty)				{					en.setMode(Mode.Cross);				}				else if (en.getMode() == Mode.Cross)				{					en.setMode(Mode.Nought);				}				else				{					en.setMode(Mode.Empty);				}				break;			}		}				repaint();	}			public void mouseEntered(MouseEvent e) { }		public void mouseExited(MouseEvent e) { }		public void mousePressed(MouseEvent e) { }		public void mouseReleased(MouseEvent e) { }}

The single biggest problem in communication is the illusion that it has taken place.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×