0% found this document useful (0 votes)
2 views2 pages

Java Tower of Hanoi Game Code

The document is a Java program that implements a version of the Tower of Hanoi game. It prompts the user to input the number of disks and manages the movement of disks between towers while ensuring valid moves. The program tracks the number of moves made until the game is completed.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Java Tower of Hanoi Game Code

The document is a Java program that implements a version of the Tower of Hanoi game. It prompts the user to input the number of disks and manages the movement of disks between towers while ensuring valid moves. The program tracks the number of moves made until the game is completed.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates


* and open the template in the editor.
*/
package javaapplication13;

import [Link];
import [Link];

/**
*
* @author kevin
*/
public class JavaApplication13 {

/**
* @param args the command line arguments
*/
@SuppressWarnings("empty-statement")
public static void main(String[] args) {
// TODO code application logic here
Scanner leer=new Scanner([Link]);
int cant_discos, torres, a,discos;
a=[Link]([Link]("Ingrese el numero de
discos: "));
a=[Link]();
while(discos<1;){
[Link]("El numero de discos debe ser mayor a 0 y menor a 5:");
a=[Link]();
}

cant_discos[0]<-discos;
cant_discos[1]<-0;
cant_discos[2]<-0;
for (i<-1 && discos ){
torres[1,i]<-discos-i+1;
}
}
cant_movs<-0;
while(cant_discos[3]<>discos){
[Link]("\033[H\033[2J");
[Link]();
for ((i<-1)=3){
[Link]("Torre ");
if(cant_discos[i]=0){
[Link]("");
}else{
for(j<-cant_discos[i];1;-1){
[Link](" xx");
[Link](" xxxxxx");
[Link](" xxxxxxxxxx");
[Link](" xxxxxxxxxxxxxx");
[Link](" xxxxxxxxxxxxxxxxxx");
[Link](" xxxxxxxxxxxxxxxxxxxxxx");
[Link](" xxxxxxxxxxxxxxxxxxxxxxxxxx");
[Link]("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
}

}
[Link]("----------------------------------");
[Link]("");
}
[Link]("Mover desde la torre: ");
Leer=t1;
[Link]("hacia la torre: ");
leer=t2;

if(t1<1;t1>3;t2<1;t2>3){
[Link]("Movimiento invalido");

}else{
disco_a_mover <- torres[t1,cant_discos[t1]]puede_mover;

if(cant_discos[t1]=0){
[Link]("Movimiento invalido");
}else{
if(cant_discos[t2]<>0){
if(torres[t2,cant_discos[t2]]<disco_a_mover){

}
if(puede_mover){
cant_movs <- cant_movs+1;
cant_discos[t2]<-cant_discos[t2]+1;
torres[t2,cant_discos[t2]] <- disco_a_mover;
cant_discos[t1]<-cant_discos[t1]-1;

}else{
[Link]("Movimiento invalido");
}
}
}
}
[Link]("\033[H\033[2J");
[Link]();
[Link]("Juego finalizado en ",cant_movs," movimientos!");
}

You might also like