Copy link to clipboard
Copied
Hello, so I have a tic tac toe game, the code was sort of given to me in a way and I wish to improve it by adding certain elements, one of which is a counter for how many times it's a tie, X wins or O wins. Although a problem is I have no idea where to put the counter and how to properly wright it the way the original code is coded, any help? Here is the code I have so far of the game :
/*
Nom fichier: U2A5_TicTacToe.as
Date: 20/04/2015
Description: Le but de cette application est de créer un
jeu de Tic_tac_toe. Elle affiche une grille
carrée de 3x3 cases. Deux joueurs s'affrontent.
Ils doivent remplir chacun à leur tour une
case de la grille avec le symbole qui leur
est attribué : O ou X. Le gagnant est celui qui
arrive à aligner trois symboles identiques,
horizontalement, verticalement ou en diagonale.
*/
package {
import flash.display.*;
import flash.events.*;
import flash.text.*;
public class U2A5_TicTacToe extends MovieClip
{
public var counterX:int;
public var counterO:int;
public var counterNul:int;
public var counterNul2:int;
// Déclarer les constantes.
private static const RANGEES:int = 3;
private static const COLONNES:int = 3;
private static const RANGEE_HAUTEUR:Number = 75;
private static const COLONNE_LARGEUR:Number = 75;
private static const RANGEE_DECALAGE:Number = 10;
private static const COLONNE_DECALAGE:Number = 10;
//Déclarer les variables.
private var caseCourante:Cases;
private var tttRangee:int;
private var tttColonne:int;
private var joueurCourant:int=2;
private var nombreDeClics:int=0;
private var joueurSymbole:String= "X";
private var gagnant:String="Début";
// Utiliser un tableau pour rester au courant des X et des O.
var rangee1:Array = [" ", " ", " "];
var rangee2:Array = [" ", " ", " "];
var rangee3:Array = [" ", " ", " "];
// Inserer les rangees dans un tableau conteneur nommer TTTPlancheJeu.
var TTTPlancheJeu:Array = [rangee1, rangee2, rangee3];
//******************************************************************
// Utiliser une fonction constructeur pour dresser la planche du jeu.
function aWC():void
{
if (gagnant == "X")
{
counterX++
trace(counterX)
}
else if (gagnant == "O")
{
counterO++
trace(counterO)
}
else
{
counterNul++
trace(counterNul)
}
}
public function U2A5_TicTacToe():void
{
for (var c:int=0; c<COLONNES; c++)
{
for (var r:int=0; r<RANGEES; r++)
{
var caseAffiche:Cases = new Cases();
caseAffiche.stop();
caseAffiche.x = c * COLONNE_LARGEUR + COLONNE_DECALAGE;
caseAffiche.y = r * RANGEE_HAUTEUR + RANGEE_DECALAGE;
addChild(caseAffiche);
caseAffiche.addEventListener(MouseEvent.CLICK, joueUnCoup);
}// Fin boucle for interne.
}// Fin boucle for externe.
}// Fin fonction constructeur.
public function joueUnCoup(event:MouseEvent)
{
nombreDeClics++;
aWC()
caseCourante = (event.target as Cases);
caseCourante.symbole = joueurCourant
caseCourante.gotoAndStop(joueurCourant);
situeSurPlancheJeu();
verifieGagnant();
changeJoueur();
}
function situeSurPlancheJeu():void
{
if (mouseX <=75)
{
tttColonne = 0
}
else if ((mouseX > 75) && (mouseX <= 150))
{
tttColonne = 1;
}
else if ((mouseX > 150) && (mouseX <= 225))
{
tttColonne = 2;
}
if (mouseY <=75)
{
tttRangee = 0;
}
else if ((mouseY > 75) && (mouseY <= 150))
{
tttRangee = 1;
}
else if ((mouseY > 150) && (mouseY <= 225))
{
tttRangee = 2;
}
TTTPlancheJeu[tttRangee][tttColonne] = joueurSymbole;
}
function verifieGagnant():void
{
// Verifier les rangees.
for (var r:int=0; r < RANGEES; r++)
{
if ( ( (TTTPlancheJeu
{
gagnant=(TTTPlancheJeu
}
}
//Verifie les colonnes
for (var c:int=0; c < COLONNES; c++)
{
if ( ( (TTTPlancheJeu[0]
{
gagnant=(TTTPlancheJeu[0]
}
}
//Verifier une des diagonales.
if ( ( (TTTPlancheJeu[0][0]) == (TTTPlancheJeu[1][1]) ) && ( (TTTPlancheJeu[1][1]) == (TTTPlancheJeu[2][2]) ) && ( (TTTPlancheJeu[0][0]) != " ") )
{
gagnant=(TTTPlancheJeu[0][0]);
}
//Verifie l'autre diagonale.
if ( ( (TTTPlancheJeu[0][2]) == (TTTPlancheJeu[1][1]) ) && ( (TTTPlancheJeu[1][1]) == (TTTPlancheJeu[2][0]) ) && ( (TTTPlancheJeu[0][2]) != " ") )
{
gagnant=(TTTPlancheJeu[0][2]);
}
//Verifier si la partie est nulle.
if ((nombreDeClics == 9) && (gagnant == "Début"))
{
gagnant = "Match nul!";
}
//S'il y a un gagnant l'indiquer.
if (gagnant != "Début")
{
MovieClip(root).gagnant = gagnant;
MovieClip(root).gotoAndStop("finPartie");
}
}// Fin fonction verifieGagnant.
function changeJoueur():void
{
if (joueurCourant == 2)
{
joueurSymbole = "O";
joueurCourant = 3;
}
else if (joueurCourant == 3)
{
joueurSymbole = "X";
joueurCourant = 2;
}
}// Fin fonction changeJoueur
}// Fin classe.
}// Fin paquetage.
Copy link to clipboard
Copied
It will help if you can identify where it is determined when the game ends and in which of the three states. If you are unable to determine that you might try going back to whoever sort of gave it to you and get their help.
Copy link to clipboard
Copied
okay, I have the counter working, only problem now is when the score is shown on screen, it adds on to the old score, so if X wins 5 times, there will be a 5 over a 4 over a 3 over a 2 over a 1. How do I get rid of the old number and just have the recent score up?
Code:
btnNouvJoute.addEventListener(MouseEvent.CLICK, joueEncore);
function joueEncore(event:MouseEvent)
{
if (gagnant == "X")
{
counterX++
}
else if (gagnant == "O")
{
counterO++
}
else
{
counterNul++
}
maMiseEnForme2.font="Arial";
maMiseEnForme2.size=16;
maMiseEnForme2.bold= false;
maMiseEnForme2.color=0xFFFFFF;
monMessage2.x=570;
monMessage2.y=45;
monMessage2.autoSize=TextFieldAutoSize.CENTER;
monMessage2.border = false;
monMessage2.defaultTextFormat=maMiseEnForme2;
monMessage2.text = " X = " + counterX +
"\r\r O = " + counterO +
"\r\r Nul = " + counterNul;
addChild(monMessage2);
Copy link to clipboard
Copied
It is not really clear what you are describing for the problem, but if I can reason it out properly you mean to say that the final score results are overlapping. To fix that just create one textfield one time instead of creating a new one every time.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now