Building a computer into tic tac toe
in
Programming Questions
•
8 months ago
Hello everyone,
I tried to build the game tic tac toe in the last few weeks and am now trying to challenge myself and my skills.
I finished tic tac toe in the sense, that two people can play it when on the same computer, but in my opinion a good game needs a singleplayer modus. So with that in mind I have a few questions.
First of all this is my code so far:
- PImage bkgd;
- PImage xp;
- PImage op;
- PFont arial;
- int turn;
- int[]grid = new int[9];
- void setup(){
- size(600,600);
- bkgd = loadImage("noughts.png");
- xp = loadImage("x.png");
- op = loadImage("o.png");
- image(bkgd,0,0);
- for (int i=0;i < grid.length;i++){
- grid[i] = 0;
- }
- turn = round(random(1));
- arial = createFont("Arial", 32);
- textFont(arial, 50);
- }
- void draw(){
- if ((grid[0] == 1 && grid[1] == 1 && grid[2] == 1) || (grid[3] == 1 && grid[4] == 1 && grid[5] == 1) || (grid[6] == 1 && grid[7] == 1 && grid[8] == 1) || (grid[0] == 1 && grid[3] == 1 && grid[6] == 1) || (grid[1] == 1 && grid[4] == 1 && grid[7] == 1) || (grid[2] == 1 && grid[5] == 1 && grid[8] == 1) || (grid[0] == 1 && grid[4] == 1 && grid[8] == 1) || (grid[6] == 1 && grid[4] == 1 && grid[2] == 1)){
- fill(255,0,0);
- text("'X' WINS",200,300);
- noLoop();
- }
- if ((grid[0] == -1 && grid[1] == -1 && grid[2] == -1) || (grid[3] == -1 && grid[4] == -1 && grid[5] == -1) || (grid[6] == -1 && grid[7] == -1 && grid[8] == -1) || (grid[0] == -1 && grid[3] == -1 && grid[6] == -1) || (grid[1] == -1 && grid[4] == -1 && grid[7] == -1) || (grid[2] == -1 && grid[5] == -1 && grid[8] == -1) || (grid[0] == -1 && grid[4] == -1 && grid[8] == -1) || (grid[6] == -1 && grid[4] == -1 && grid[2] == -1)){
- fill(255,0,0);
- text("'O' WINS",200,300);
- noLoop();
- }
- }
- void mousePressed(){
- x();
- o();
- }
- void keyPressed(){
- if (key == 'r'){
- image(bkgd,0,0);
- for (int i=0;i < grid.length;i++){
- grid[i] = 0;
- }
- turn = round(random(1));
- arial = createFont("Arial", 32);
- textFont(arial, 50);
- loop();
- }
- }
- void x(){
- if (mouseButton == LEFT){
- if (mouseX > 0 && mouseX < 200 && mouseY > 0 && mouseY < 200 && grid[0] == 0 && turn == 1){
- image(xp,50,50);
- grid[0] = 1;
- turn = 0;
- }
- if (mouseX > 0 && mouseX < 200 && mouseY > 200 && mouseY < 400 && grid[1] == 0 && turn == 1){
- image(xp,50,250);
- grid[1] = 1;
- turn = 0;
- }
- if (mouseX > 0 && mouseX < 200 && mouseY > 400 && mouseY < 600 && grid[2] == 0 && turn == 1){
- image(xp,50,450);
- grid[2] = 1;
- turn = 0;
- }
- if (mouseX > 200 && mouseX < 400 && mouseY > 0 && mouseY < 200 && grid[3] == 0 && turn == 1){
- image(xp,250,50);
- grid[3] = 1;
- turn = 0;
- }
- if (mouseX > 200 && mouseX < 400 && mouseY > 200 && mouseY < 400 && grid[4] == 0 && turn == 1){
- image(xp,250,250);
- grid[4] = 1;
- turn = 0;
- }
- if (mouseX > 200 && mouseX < 400 && mouseY > 400 && mouseY < 600 && grid[5] == 0 && turn == 1){
- image(xp,250,450);
- grid[5] = 1;
- turn = 0;
- }
- if (mouseX > 400 && mouseX < 600 && mouseY > 0 && mouseY < 200 && grid[6] == 0 && turn == 1){
- image(xp,450,50);
- grid[6] = 1;
- turn = 0;
- }
- if (mouseX > 400 && mouseX < 600 && mouseY > 200 && mouseY < 400 && grid[7] == 0 && turn == 1){
- image(xp,450,250);
- grid[7] = 1;
- turn = 0;
- }
- if (mouseX > 400 && mouseX < 600 && mouseY > 400 && mouseY < 600 && grid[8] == 0 && turn == 1){
- image(xp,450,450);
- grid[8] = 1;
- turn = 0;
- }
- }
- }
- void o(){
- if (mouseButton == RIGHT){
- if (mouseX > 0 && mouseX < 200 && mouseY > 0 && mouseY < 200 && grid[0] == 0 && turn == 0){
- image(op,50,50);
- grid[0] = -1;
- turn = 1;
- }
- if (mouseX > 0 && mouseX < 200 && mouseY > 200 && mouseY < 400 && grid[1] == 0 && turn == 0){
- image(op,50,250);
- grid[1] = -1;
- turn = 1;
- }
- if (mouseX > 0 && mouseX < 200 && mouseY > 400 && mouseY < 600 && grid[2] == 0 && turn == 0){
- image(op,50,450);
- grid[2] = -1;
- turn = 1;
- }
- if (mouseX > 200 && mouseX < 400 && mouseY > 0 && mouseY < 200 && grid[3] == 0 && turn == 0){
- image(op,250,50);
- grid[3] = -1;
- turn = 1;
- }
- if (mouseX > 200 && mouseX < 400 && mouseY > 200 && mouseY < 400 && grid[4] == 0 && turn == 0){
- image(op,250,250);
- grid[4] = -1;
- turn = 1;
- }
- if (mouseX > 200 && mouseX < 400 && mouseY > 400 && mouseY < 600 && grid[5] == 0 && turn == 0){
- image(op,250,450);
- grid[5] = -1;
- turn = 1;
- }
- if (mouseX > 400 && mouseX < 600 && mouseY > 0 && mouseY < 200 && grid[6] == 0 && turn == 0){
- image(op,450,50);
- grid[6] = -1;
- turn = 1;
- }
- if (mouseX > 400 && mouseX < 600 && mouseY > 200 && mouseY < 400 && grid[7] == 0 && turn == 0){
- image(op,450,250);
- grid[7] = -1;
- turn = 1;
- }
- if (mouseX > 400 && mouseX < 600 && mouseY > 400 && mouseY < 600 && grid[8] == 0 && turn == 0){
- image(op,450,450);
- grid[8] = -1;
- turn = 1;
- }
- }
- }
As you can see I haven't started the computer yet. My problem is that I don't want to start and after that notice that I did way too much work.
I thought about making the computer finish a line if it has already two of its own in there.
- if ((grid[0] + grid[1]) == -2 && turn == 0){
- image(op,50,450);
- grid[2] = -1;
- turn = 1;
- }
As you can see my try is not really efficient as I had to code the same thing for every possible combination of two. My question now is, if there is an easier way to do the whole thing.
Thanks for any help!
Sincerely SihingSin
1