tic tac toe?
in
Programming Questions
•
3 years ago
I have to put a together a game of tic tact toe via processing for a class, but I'm pretty stumped.
The trick is that we have to use a search and storage algorithm which was given to us;
int pX = map(mouseX, 0, width, 0, gridwidth);
int pY = map(mouseY, 0, height, 0, gridheight);
Any help regarding this would be greatly appreciated, the code I have so far is below.
--------------------------------------
//Declarations
char alphabet[]= {
'X', 'O', 'X', 'O', 'X', 'O', 'X', 'O', 'X'};
PFont dFont;
//grid
int gridWidth = 3;
int gridHeight = 3;
//players
int playCounter = 0;
// x=1, O=0
int currentPlayer = 1;
void setup(){
size(300,300);
background(255);
noCursor();
//set style
dFont = loadFont("ActionJackson-48.vlw");
textFont(dFont);
textAlign(LEFT,TOP);
}//end setup
void draw (){
background (255);
drawGrid();
readArray();
checkwin();
alternatePlayer();
//map mouse location (in pixels) to a grid location
int gridRow = (int) map(mouseX, 0, width, 0, gridWidth);
int gridCol = (int) map(mouseY, 0, height, 0, gridHeight);
//convert grid location to array location to
//read which character to display
int index= (gridRow * gridWidth) + gridCol;
//text style parameters
/*fill(0);
textAlign(CENTER, CENTER);
text(alphabet[index], mouseX, mouseY);
*/
}//end draw
//the grid
void drawGrid(){
stroke(0);
strokeWeight(5);
line (100,0,100,300);
line (200,0,200,300);
line (0,100,300,100);
line (0,200,300,200);
}
void readArray(){
//searching 2d space and convertin it to 2D spaace
for (int gridRow = 0; gridRow < gridHeight; gridRow ++){
for (int gridCol = 0; gridCol < gridWidth; gridCol++){
//convert grid location to array location
int index = (gridRow * gridWidth) + gridCol;
//output to text window
// print(alphabet[index]);
//translate grid location to pixel location
int pixelX= (int) map(gridRow, 0, gridWidth, 0, width);
int pixelY= (int) map(gridCol, 0, gridHeight, 0, height);
//text
//fill(0);
//text (alphabet[index], pixelX, pixelY);
}//end gridCol
}//end gridRow
}
void checkwin(){
// check to see if any COLUMNS are filled with same charcter
// columns are indexed -- > 0,1,2 .. gridWidth
for (int i = 0; i < gridWidth; i++){
if ( (alphabet[i] != ' ')
&& (alphabet[i] == alphabet[ i + 1 * gridWidth])
&& (alphabet[i] == alphabet[ i + 2 * gridWidth]) )
{
print("column " + i + " is all " + alphabet[i]);
} // end if
} // end for
// check to see if any ROWS are filled with same charcter
// you must look in both cells of the row
// the first cell in each rows is INDEXED as a multiple of gridWidth
// therefore the iterator <i> is multiplied by gridWidth -- this ensures that
// your search begins at the start of each row and carries on across
for (int i = 0; i < gridHeight; i++){
if ( (alphabet[i*gridWidth] != ' ')
&& (alphabet[i*gridWidth] == alphabet[i*gridWidth+1]) )
{
println("\trow " + i + " is all " + alphabet[i*gridWidth]);
} // end if
}
}
void alternatePlayer(){
if (currentPlayer == 1) {
println("currentplayer is X");
fill(0);
text("X", mouseX, mouseY);
}
if (currentPlayer == 0) {
println("currentplayer is O");
text("O", mouseX, mouseY);
}
}
void mousePressed(){
currentPlayer = 1 - currentPlayer;
if (currentPlayer == 1) {
fill(0);
text("X", mouseX, mouseY);
}
if (currentPlayer == 0) {
text("O", mouseX, mouseY);
}
}
The trick is that we have to use a search and storage algorithm which was given to us;
int pX = map(mouseX, 0, width, 0, gridwidth);
int pY = map(mouseY, 0, height, 0, gridheight);
Any help regarding this would be greatly appreciated, the code I have so far is below.
--------------------------------------
//Declarations
char alphabet[]= {
'X', 'O', 'X', 'O', 'X', 'O', 'X', 'O', 'X'};
PFont dFont;
//grid
int gridWidth = 3;
int gridHeight = 3;
//players
int playCounter = 0;
// x=1, O=0
int currentPlayer = 1;
void setup(){
size(300,300);
background(255);
noCursor();
//set style
dFont = loadFont("ActionJackson-48.vlw");
textFont(dFont);
textAlign(LEFT,TOP);
}//end setup
void draw (){
background (255);
drawGrid();
readArray();
checkwin();
alternatePlayer();
//map mouse location (in pixels) to a grid location
int gridRow = (int) map(mouseX, 0, width, 0, gridWidth);
int gridCol = (int) map(mouseY, 0, height, 0, gridHeight);
//convert grid location to array location to
//read which character to display
int index= (gridRow * gridWidth) + gridCol;
//text style parameters
/*fill(0);
textAlign(CENTER, CENTER);
text(alphabet[index], mouseX, mouseY);
*/
}//end draw
//the grid
void drawGrid(){
stroke(0);
strokeWeight(5);
line (100,0,100,300);
line (200,0,200,300);
line (0,100,300,100);
line (0,200,300,200);
}
void readArray(){
//searching 2d space and convertin it to 2D spaace
for (int gridRow = 0; gridRow < gridHeight; gridRow ++){
for (int gridCol = 0; gridCol < gridWidth; gridCol++){
//convert grid location to array location
int index = (gridRow * gridWidth) + gridCol;
//output to text window
// print(alphabet[index]);
//translate grid location to pixel location
int pixelX= (int) map(gridRow, 0, gridWidth, 0, width);
int pixelY= (int) map(gridCol, 0, gridHeight, 0, height);
//text
//fill(0);
//text (alphabet[index], pixelX, pixelY);
}//end gridCol
}//end gridRow
}
void checkwin(){
// check to see if any COLUMNS are filled with same charcter
// columns are indexed -- > 0,1,2 .. gridWidth
for (int i = 0; i < gridWidth; i++){
if ( (alphabet[i] != ' ')
&& (alphabet[i] == alphabet[ i + 1 * gridWidth])
&& (alphabet[i] == alphabet[ i + 2 * gridWidth]) )
{
print("column " + i + " is all " + alphabet[i]);
} // end if
} // end for
// check to see if any ROWS are filled with same charcter
// you must look in both cells of the row
// the first cell in each rows is INDEXED as a multiple of gridWidth
// therefore the iterator <i> is multiplied by gridWidth -- this ensures that
// your search begins at the start of each row and carries on across
for (int i = 0; i < gridHeight; i++){
if ( (alphabet[i*gridWidth] != ' ')
&& (alphabet[i*gridWidth] == alphabet[i*gridWidth+1]) )
{
println("\trow " + i + " is all " + alphabet[i*gridWidth]);
} // end if
}
}
void alternatePlayer(){
if (currentPlayer == 1) {
println("currentplayer is X");
fill(0);
text("X", mouseX, mouseY);
}
if (currentPlayer == 0) {
println("currentplayer is O");
text("O", mouseX, mouseY);
}
}
void mousePressed(){
currentPlayer = 1 - currentPlayer;
if (currentPlayer == 1) {
fill(0);
text("X", mouseX, mouseY);
}
if (currentPlayer == 0) {
text("O", mouseX, mouseY);
}
}
1