Codigo fuente del juego de gato en Java?

Hola, alguien tiene el codigo fuente para hacer el juego del gato??, ese que pones cruz o tache segun sea lo que te toque, agradecera mucho a quien me pueda ayudar

1 respuesta

Calificación
  • hace 1 década
    Mejor Respuesta

    package michi;

    public class CMichi {

    int turno;

    CJugador Jugador1;

    CJugador Jugador2;

    CTablero Tablero;

    String Ganador;

    public CMichi() {

    Jugador1 = new CJugador("1", "X");

    Jugador2 = new CJugador("2", "O");

    Tablero = new CTablero();

    Tablero.cleartable();

    turno = 0;

    Ganador = "";

    }

    public void Nuevo() {

    Tablero.cleartable();

    turno = 0;

    Ganador = "";

    }

    public String Jugar(int i, int j){

    if ( Tablero.is_null(i,j) ) {

    if (turno == 0) {

    Tablero.Marcar(i, j, Jugador1.getSymbol());

    if (Tablero.Gano(Jugador1.getSymbol()) != 0) {

    Ganador = Jugador1.getNombre();

    }

    turno = 1;

    return Jugador1.getSymbol();

    }

    else {

    Tablero.Marcar(i, j, Jugador2.getSymbol());

    if (Tablero.Gano(Jugador2.getSymbol()) != 0) {

    Ganador = Jugador2.getNombre();

    }

    turno = 0;

    return Jugador2.getSymbol();

    }

    }

    else {

    return "";

    }

    }

    public String getGanador() {

    return this.Ganador;

    }

    public String getTablero() {

    return Tablero.getTablero();

    }

    }

    CTablero

    CÓDIGO

    package michi;

    public class CTablero {

    String[][] Tablero = new String[3][3];

    public CTablero() {

    }

    public void cleartable(){

    for(int i=0; i < 3; i++){

    for(int j=0; j < 3; j++){

    Tablero[i][j] = "";

    }

    }

    }

    public boolean is_null(int x, int y) {

    if ( Tablero[x][y] == "" ) {

    return true;

    }

    else {

    return false;

    }

    }

    public void Marcar(int x, int y, String symbol) {

    Tablero[x][y] = symbol;

    }

    public boolean Fila(int fila, String symbol) {

    int cont = 0;

    for(int i=0; i < 3; i++){

    if ( Tablero[fila][i] == symbol ) {

    cont += 1;

    }

    }

    if ( cont == 3 ) {

    return true;

    }

    else {

    return false;

    }

    }

    public boolean Columna(int col, String symbol) {

    int cont = 0;

    for(int i=0; i < 3; i++){

    if ( Tablero[i][col] == symbol ) {

    cont += 1;

    }

    }

    if ( cont == 3 ) {

    return true;

    }

    else {

    return false;

    }

    }

    public int Gano(String symbol){

    if ( Fila(0,symbol) ) {

    return 1;

    }

    if ( Fila(1,symbol) ) {

    return 2;

    }

    if ( Fila(2,symbol) ) {

    return 3;

    }

    if ( Columna(0,symbol) ) {

    return 4;

    }

    if ( Columna(1,symbol) ) {

    return 5;

    }

    if ( Columna(2,symbol) ) {

    return 6;

    }

    if ( (Tablero[0][0] == symbol) && (Tablero[1][1] == symbol) && (Tablero[2][2] == symbol) ) {

    return 7;

    }

    if ( (Tablero[0][2] == symbol) && (Tablero[1][1] == symbol) && (Tablero[2][0] == symbol) ) {

    return 8;

    }

    return 0;

    }

    public String getTablero() {

    String valor = "";

    for(int i=0; i < 3; i++){

    for(int j=0; j < 3; j++){

    if (Tablero[i][j] == "") {

    valor += "-";

    }else{

    valor += Tablero[i][j];

    }

    }

    }

    return valor;

    }

    }

    O SI NO ESTE

    import java.awt.*;

    import java.awt.event.*;

    import java.awt.image.*;

    import java.net.*;

    import java.applet.*;

    public

    class asd extends Applet implements MouseListener {

    /**

    * White's current position. The computer is white.

    */

    int white;

    /**

    * Black's current position. The user is black.

    */

    int black;

    /**

    * The squares in order of importance...

    */

    final static int moves[] = {4, 0, 2, 6, 8, 1, 3, 5, 7};

    /**

    * The winning positions.

    */

    static boolean won[] = new boolean[1 << 9];

    static final int DONE = (1 << 9) - 1;

    static final int OK = 0;

    static final int WIN = 1;

    static final int LOSE = 2;

    static final int STALEMATE = 3;

    /**

    * Mark all positions with these bits set as winning.

    */

    static void isWon(int pos) {

    for (int i = 0 ; i < DONE ; i++) {

    if ((i & pos) == pos) {

    won[i] = true;

    }

    }

    }

    /**

    * Initialize all winning positions.

    */

    static {

    isWon((1 << 0) | (1 << 1) | (1 << 2));

    isWon((1 << 3) | (1 << 4) | (1 << 5));

    isWon((1 << 6) | (1 << 7) | (1 << 8));

    isWon((1 << 0) | (1 << 3) | (1 << 6));

    isWon((1 << 1) | (1 << 4) | (1 << 7));

    isWon((1 << 2) | (1 << 5) | (1 << 8));

    isWon((1 << 0) | (1 << 4) | (1 << 8));

    isWon((1 << 2) | (1 << 4) | (1 << 6));

    }

    /**

    * Compute the best move for white.

    * @return the square to take

    */

    int bestMove(int white, int black) {

    int bestmove = -1;

    loop:

    for (int i = 0 ; i < 9 ; i++) {

    int mw = moves[i];

    if (((white & (1 << mw)) == 0) && ((black & (1 << mw)) == 0)) {

    int pw = white | (1 << mw);

    if (won[pw]) {

    // white wins, take it!

    return mw;

    }

    for (int mb = 0 ; mb < 9 ; mb++) {

    if (((pw & (1 << mb)) == 0) && ((black & (1 << mb)) == 0)) {

    int pb = black | (1 << mb);

    if (won[pb]) {

    // black wins, take another

    continue loop;

    }

    }

    }

    // Neither white nor black can win in one move, this will do.

    if (bestmove == -1) {

    bestmove = mw;

    }

    }

    }

    if (bestmove != -1) {

    return bestmove;

    }

    // No move is totally satisfactory, try the first one that is open

    for (int i = 0 ; i < 9 ; i++) {

    int mw = moves[i];

    if (((white & (1 << mw)) == 0) && ((black & (1 << mw)) == 0)) {

    return mw;

    }

    }

    // No more moves

    return -1;

    }

    /**

    * User move.

    * @return true if legal

    */

    boolean yourMove(int m) {

    if ((m < 0) || (m > 8)) {

    return false;

    }

    if (((black | white) & (1 << m)) != 0) {

    return false;

    }

    black |= 1 << m;

    return true;

    }

    /**

    * Computer move.

    * @return true if legal

    */

    boolean myMove() {

    if ((black | white) == DONE) {

    return false;

    }

    int best = bestMove(white, black);

    white |= 1 << best;

    return true;

    }

    /**

    * Figure what the status of the game is.

    */

    int status() {

    if (won[white]) {

    return WIN;

    }

    if (won[black]) {

    return LOSE;

    }

    if ((black | white) == DONE) {

    return STALEMATE;

    }

    return OK;

    }

    /**

    * Who goes first in the next game?

    */

    boolean first = true;

    /**

    * The image for white.

    */

    Image notImage;

    /**

    * The image for black.

    */

    Image crossImage;

    /**

    * Initialize the applet. Resize and load images.

    */

    public void init() {

    notImage = getImage(getCodeBase(), "images/not.gif");

    crossImage = getImage(getCodeBase(), "images/cross.gif");

    addMouseListener(this);

    }

    public void destroy() {

    removeMouseListener(this);

    }

    /**

    * Paint it.

    */

    public void paint(Graphics g) {

    Dimension d = getSize();

    g.setColor(Color.black);

    int xoff = d.width / 3;

    int yoff = d.height / 3;

    g.drawLine(xoff, 0, xoff, d.height);

    g.drawLine(2*xoff, 0, 2*xoff, d.height);

    g.drawLine(0, yoff, d.width, yoff);

    g.drawLine(0, 2*yoff, d.width, 2*yoff);

    int i = 0;

    for (int r = 0 ; r < 3 ; r++) {

    for (int c = 0 ; c < 3 ; c++, i++) {

    if ((white & (1 << i)) != 0) {

    g.drawImage(notImage, c*xoff + 1, r*yoff + 1, this);

    } else if ((black & (1 << i)) != 0) {

    g.drawImage(crossImage, c*xoff + 1, r*yoff + 1, this);

    }

    }

    }

    }

    /**

    * The user has clicked in the applet. Figure out where

    * and see if a legal move is possible. If it is a legal

    * move, respond with a legal move (if possible).

    */

    public void mouseReleased(MouseEvent e) {

    int x = e.getX();

    int y = e.getY();

    switch (status()) {

    case WIN:

    case LOSE:

    case STALEMATE:

    play(getCodeBase(), "audio/return.au");

    white = black = 0;

    if (first) {

    white |= 1 << (int)(Math.random() * 9);

    }

    first = !first;

    repaint();

    return;

    }

    // Figure out the row/column

    Dimension d = getSize();

    int c = (x * 3) / d.width;

    int r = (y * 3) / d.height;

    if (yourMove(c + r * 3)) {

    repaint();

    switch (status()) {

    case WIN:

    play(getCodeBase(), "audio/yahoo1.au");

    break;

    case LOSE:

    play(getCodeBase(), "audio/yahoo2.au");

    break;

    case STALEMATE:

    break;

    default:

    if (myMove()) {

    repaint();

    switch (status()) {

    case WIN:

    play(getCodeBase(), "audio/yahoo1.au");

    break;

    case LOSE:

    play(getCodeBase(), "audio/yahoo2.au");

    break;

¿Aún tienes preguntas? Pregunta ahora para obtener respuestas.