Advertisement
Guest User

Untitled

a guest
Mar 6th, 2015
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.09 KB | None | 0 0
  1. package pkg4ps;
  2.  
  3. /**
  4.  *
  5.  * @author Jonas N. Nielsen
  6.  */
  7. public class Stribe {
  8.        
  9.     public static String Spiller;
  10.     public static String spiller1;
  11.     public static String spiller2;
  12.     public static int tur=2;
  13.     public static int Vinder = 0; // 0 = Spiller, 1 = vinder 2 = uafgjort
  14.     public static int[][]spil = new int[6][7];
  15.     public static int[] brik = new int[7];
  16.  
  17. public static void main(String[] args)
  18. {
  19.     //introduktion
  20.     System.out.println("Hej og velkommen til Fire Paa Stribe");
  21.     System.out.print("Indtast venligt navn til spiller 1: ");
  22.     spiller1 = Keyboard.readString();
  23.     System.out.print("Indtast venligt navn til spiller 2: ");
  24.     spiller2 = Keyboard.readString();
  25.     while (Vinder == 0)
  26.     {
  27.         print();
  28.         placering();
  29.     }
  30.     switch (Vinder)
  31.     {//Switch der udrinter case 1 hvis at der er en vinder
  32.      //eller case 2 hvis braettet bliver fyldt (uafgjort)
  33.         case 1: System.out.println("Tilykke " + Spiller + " du har vundet!");
  34.                 break;
  35.         case 2: System.out.println("Spillet endte uafgjort");
  36.                 break;
  37.     }
  38. }
  39.            
  40. public static void print ()
  41. {
  42.     {//dannelse af array
  43.     for (int r=5;r>=0;r--)
  44.     {
  45.         for (int s=0;s<7;s++)
  46.         {
  47.             if (spil[r][s] == 0) System.out.print (" - ");
  48.             else if (spil[r][s] == 1) System.out.print (" O ");
  49.             else if (spil[r][s] == 2) System.out.print (" X ");
  50.             else System.out.print(" "+spil[r][s]+" ");
  51.         }
  52.         System.out.println();
  53.     }//ekstra linje under spillet for at indikere soejlenummer
  54. System.out.println(" 1  2  3  4  5  6  7\n");
  55.     }
  56. }
  57.  
  58. public static void placering ()
  59. {
  60.     int s;
  61.     while (Tjek())
  62.         {//loekke der skifter mellem spiller 1 og 2
  63.             if (tur == 2)
  64.             {
  65.                 tur = 1;
  66.                 Spiller = spiller1;
  67.             }
  68.             else
  69.             {
  70.                 tur = 2;
  71.                 Spiller = spiller2;
  72.             }
  73.             while (true)
  74.             {//sikring der gor at man ikke kan placere brikker uden for spillet
  75.                 System.out.println("Det er "+Spiller+"'s tur\n");
  76.                 s = Keyboard.readInt();
  77.                 if ((s > 0 && s < 8) && (brik[s-1] < 6))
  78.                 {
  79.                     break;
  80.                 }
  81.             }
  82.             s = s - 1;
  83.            
  84.             spil[brik[s]++][s]=tur;
  85.             print();
  86.         }
  87. }
  88.  
  89. public static boolean Tjek ()
  90. {
  91.     // diagonalt
  92.     for (int s=0; s<4; s++)
  93.     {
  94.         for (int r=0; r<3; r++)
  95.         {
  96.     int a = spil[r][s];
  97.     int b = spil[r][s+3];
  98.  
  99.             if ((a != 0) && (spil[r+1][s+1] == a) && (spil[r+2][s+2] == a) && (spil[r+3][s+3] == a))
  100.         Vinder = 1;
  101.             else if ((b != 0) && (spil[r+1][s+2] == b) && (spil[r+2][s+1] == b) && (spil[r+3][s] == b))
  102.         Vinder = 1;
  103.         }
  104.     }
  105.     // horisontalt
  106.     for (int s=0; s<4; s++)
  107.     {
  108.         for (int r=0; r<6; r++)
  109.         {
  110.             int c = spil[r][s];
  111.             if (c == 0)
  112.             {
  113.                 break;
  114.             }
  115.             if (spil[r][s] == 0)
  116.             {
  117.                 break;
  118.             }
  119.             if ((spil[r][s+1] == c) && (spil[r][s+2] == c) && (spil[r][s+3] == c))
  120.                 Vinder = 1;
  121.         }
  122.     }
  123.     // vertikalt
  124.     for (int s=0; s<6; s++)
  125.     {
  126.         for (int r=0; r<3; r++)
  127.         {
  128.             int c = spil[r][s];
  129.             if (c == 0)
  130.             {
  131.                 break;
  132.             }
  133.             if (spil[r][s] == 0)
  134.             {
  135.                 break;
  136.             }
  137.             if ((spil[r+1][s] == c) && (spil[r+2][s] == c) && (spil[r+3][s] == c))
  138.                 Vinder = 1;
  139.         }
  140.     }
  141.     if (Vinder == 1)
  142.     {
  143.         return false;
  144.     }
  145.     else
  146.         return !fuld();
  147. }
  148. public static boolean fuld ()
  149. {
  150.     for (int r=0; r<6; r++)
  151.     {
  152.            for (int s=0; s<7; s++)
  153.            {
  154.                if (spil[r][s] == 0)
  155.                    return false;
  156.            }
  157.     }
  158.     Vinder = 2;
  159.     return true;
  160. }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement