Snake and Ladder game In java

আমরা ছোট বেলায় অনেকেই সাপ লুডু খেলেছি এবং এখনো খেলতে পছন্দ করি। যুগ বদলেছে আজ আমরা মাঠে খেলার বদলে খেলি কম্পিউটার এর ছোট্টখানি পর্দায়। তাই আমিও চেস্টা করেছি আমাদের ছোট্ট বেলার সেই খেলাটিকে ডিজিটাল পর্দায় নিয়ে আসতে। যদিও আমার এই খেলাটি অত্যন্ত সাধারণ মানের এবং এর কোন গ্রাফিক্যাল অস্তিত্ব নেই তবুও এটা কিন্তু ডিজিটাল।

কোড সমূহঃ

GameCore.java

import java.util.Random;
import java.util.Scanner;
public class GameCore{
  int a1=0,i1=0,temp1=0,d=1;
  Scanner input = new Scanner(System.in);
  Random randomNumbers = new Random();
  public int Game(){
    int a=a1, i = i1, temp=temp1;
    while(i<100){
      while((i>=94)&&(i<100)){
        if(d>=2){
          int k = i;
          System.out.println("Enter 1 to roll");
          int n = input.nextInt();
          if(n==1){
            a = 1 + randomNumbers.nextInt(6);
          }else{
            while(n!=1){
              System.out.println("Please Enter a valid command");
              n = input.nextInt();
            }
          }
          System.out.println("You have rolled "+a);
          k = k+a;
          if((k>100)&&(k!=100)){
            k = i;
          }else{
            i=k;
            System.out.println("You are now in "+i);
          }
          if (i==97){
            System.out.println("You are on the mouth of a snake");
            temp = i;
            i = 16;
            System.out.println("You are now in "+i+" from "+temp);
          }
        }
        ++d;
        a1=a;i1=i;temp1=temp;
        return 1;
      }
      System.out.println("Enter 1 to roll");
      int n = input.nextInt();
      if(n==1){
        a = 1 + randomNumbers.nextInt(6);
      }else{while(n!=1){
        System.out.println("Please Enter valid command");
        n = input.nextInt();
      }
      }
      System.out.println("You have rolled "+a);
      i=i+a;
      System.out.println("Your position "+i);
      if (i==8){
        System.out.println("You are on a ladder");
        temp = i;
        i = 31;
        System.out.println("You are now in "+i+" from "+temp);
      }
      if (i==13){
        System.out.println("You are on the mouth of a snake");
        temp = i;
        i = 1;
        System.out.println("You are now in "+i+" from "+temp);
      }
      if (i==24){
        System.out.println("You are on the mouth of a snake");
        temp = i;
        i = 2;
        System.out.println("You are now in "+i+" from "+temp);
      }
      if (i==15){
        System.out.println("You are on a ladder");
        temp = i;
        i = 98;
        System.out.println("You are now in "+i+" from "+temp);
      }
      if (i==45){
        System.out.println("You are on the mouth of a snake");
        temp = i;
        i = 16;
        System.out.println("You are now in "+i+" from "+temp);
      }
      if (i==42){
        System.out.println("You are on a ladder");
        temp = i;
        i = 81;
        System.out.println("You are now in "+i+" from "+temp);
      }
      if (i==55){
        System.out.println("You are on the mouth of a snake");
        temp = i;
        i = 12;
        System.out.println("You are now in "+i+" from "+temp);
      }
      if (i==66){
        System.out.println("You are on a ladder");
        temp = i;
        i = 87;
        System.out.println("You are now in "+i+" from "+temp);
      }
      if (i==88){
        System.out.println("You are on the mouth of a snake");
        temp = i;
        i = 67;
        System.out.println("You are now in "+i+" from "+temp);
      }
      while((i>=94)&&(i<100)){
        if(d>=2){
          int k = i;
          System.out.println("Enter 1 to roll");
          n = input.nextInt();
          if(n==1){
            a = 1 + randomNumbers.nextInt(6);
          }else{
            while(n!=1){
              System.out.println("Please Enter a valid command");
              n = input.nextInt();
            }
          }
          System.out.println("You have rolled "+a);
          k = k+a;
          if((k>100)&&(k!=100)){
            k = i;
          }else{
            i=k;
            System.out.println("You are now in "+i);
          }
          int t = 0;
          while(t<1){
            if (i==97){
              System.out.println("You are on the mouth of a snake");
              temp = i;
              i = 16;
              System.out.println("You are now in "+i+" from "+temp);
            }
            if (i==95){
              System.out.println("You are on the mouth of a snake");
              temp = i;
              i = 10;
              System.out.println("You are now in "+i+" from "+temp);
            }
          }
        }
        ++d;
        a1=a;i1=i;temp1=temp;
        return 1;
      }
      a1=a;i1=i;temp1=temp;

      return 1;
    }
    System.out.println("You have reached the finish line!");
    return 1;
  }
}

PlayGame.java


public class PlayGame{
  public static void main (String [] args){
    System.out.println("Welcome To the Game of Snake and ladder");
    System.out.println("             ____________");
    System.out.println("############/ ___________/#########################");
    System.out.println("###########//######################################");
    System.out.println("##########//#######################################");
    System.out.println("#########//########################################");
    System.out.println("########//#########################################");
    System.out.println("#######//##########################################");
    System.out.println("######//___________################################");
    System.out.println("#####/___________ /################################");
    System.out.println("################//#################################");
    System.out.println("###############//##################################");
    System.out.println("##############//####################//##########//#");
    System.out.println("#############//####################//==========//##");
    System.out.println("############//####################//##########//###");
    System.out.println("#__________//####################//##########//####");
    System.out.println("/__________/####################//==========//#####");
    System.out.println("###############################//##########//######");
    System.out.println("##############################//##########//#######");
    System.out.println("#############################//##########//########");
    System.out.println("############################//==========//#########");
    System.out.println("###########################//##########//##########");
    System.out.println("##########################//##########//###########");
    System.out.println("#########################//##########//############");
    System.out.println("########################//==========//#############");
    System.out.println("#######################//##########//##############");
    System.out.println("######################//##########//###############");
    System.out.println("#####################//##########//################");
    System.out.println("####################//==========//#################");
    System.out.println("###################//##########//##################");
    System.out.println("###################################################");
    System.out.println("###################################################");
    System.out.println("###################################################");
    System.out.println("######################################ANIK#DAS#####");
    System.out.println("###################################################");

    GameCore player1 = new GameCore();
    GameCore player2 = new GameCore();
    int p = 0, i=0;
    while(p<3){
      p=1;
      if(p==1){
        System.out.println("Turn For Player 1");
        player1.Game();
        System.out.println("_________________");
      }
      i=0;
      while(i<1){
        if(player1.i1>=100){
          System.out.println("Player 1 wins");
          p = 3;
        }
        if(player2.i1>=100){
          System.out.println("Player 2 wins");
          p = 3;
        }
        ++i;
      }
      if(p<3){
        p=2;
        if(p==2){
          System.out.println("Turn For Player 2");
          player2.Game();
          System.out.println("_________________");
        }
        i=0;
        while(i<1){
          if(player1.i1>=100){
            System.out.println("Player 1 wins");
            p = 3;
          }
          if(player2.i1>=100){
            System.out.println("Player 2 wins");
            p = 3;
          }
          ++i;
        }
      }
    }
  }
}

প্রথমে আপনার জাভা client এ কোড দুটো পেস্ট করুন তারপর compile করুন। PlayGame.java ফাইল টি রান করুন। যেহেতু গেমটি দুজন মিলে খেলতে হয় তাই আপনার একজন সঙ্গী দরকার হবে। দুজনে মিলে উপভোগ করুন আমার এই ছোট্ট গেমটি… 🙂

About anik

This is Anik Das. I am Student of Electrical and Electronics engineering in National Institute of Technology, Warangal (NITW). I used to study in BRAC University(BRACU) (which is one of the best part of my life). There I also studied EEE but after getting the ICCR Scholarship by the Indian Govt. I moved to NITW. I have completed my H.S.C from Notre Dame College (NDC). I used to work as Microsoft Student Partner(MSP) in Microsoft. I am a tech loving boy…I also love to know about newer and newer things. I am here to share what I learn...

Posted on May 5, 2011, in java, java program and tagged , , , , . Bookmark the permalink. 4 Comments.

  1. simply, wow.

  2. dst its awsome.genius 🙂

  3. u r an awesome coder….hats off 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: