Russisches Roulette

Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

#define ANZ 6
#define loose "Ruh in Frieden!\n"
#define win "Dein PC ist von uns gegangen!\n"

struct pl
 {
  int pc, user, who;
  short where;
 } game;
 
void uwahl(struct pl*);
void wahl(struct pl*);
void shoot(struct pl*);
short drehen(void);

int main(int argc, char *argv[])
{
  
 game.pc=1;
 game.user=1;
 
 
 srand((unsigned)time(NULL));
 game.where=drehen();
 
 game.who=(short)ceil((((double)rand()/RAND_MAX)*2)-1);
 
 do
 {
 if(game.who)
 {
  uwahl(&game);
  shoot(&game);
  game.who=0;
 }
 else
 {
  wahl(&game);
  shoot(&game);
  game.who=1;
 }
 }while(game.user&&game.pc);
 
 if(!game.user)
 {
  printf("%s", loose);
 }
 else
 {
  printf("%s", win);
 }
 
 system("PAUSE");	
 return 0;
}

short drehen(void)
{
 return (short)ceil((((double)rand()/RAND_MAX)*6)-1);
}

void shoot(struct pl *foobar)
{
 if(foobar->where==0)
 {
  if(foobar->who)
   foobar->user=0;
  else
   foobar->pc=0;
 }
 else
  foobar->where--;
}

void uwahl(struct pl *foo)
{
 short in;
 printf("1 Druecken um zu drehen \n");
 scanf("%u", &in);
 if(in==1)
  foo->where=drehen();
}

void wahl(struct pl *bar)
{
 short wahl=(short)ceil((((double)rand()/RAND_MAX)*2)-1);
 if(wahl)
  {
   printf("PC dreht! \n");
   bar->where=drehen();
  }
 else
  printf("Der Pc schiesst direkt!\n");
}

Bisschen lang aber es tut seine Aufgabe. :]

P.S: Bitte keine Buchstaben eingeben!
 
Hoffe es klappt bei jedem.
Ich konnte es unter g++ compilen. Wenn man es unter Windows compilen moechte, muss man die Funktion system("clear"); durch system("cls"); ersetzen.

Code:
/*
  Russisches Roulette
*/

#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

using namespace std;

void newGame(int &kugelPos , bool &turnFlag);
void spin(int &kugelPos);
bool shoot(int &kugelPos , bool &turnFlag);
bool turn(int &kugelPos , bool &turnFlag);

int main()
{
  int kugelPos;
  bool turnFlag , // Wer ist an der Reihe? true=Spieler , false=Computer
       gameFlag=true;

  newGame(kugelPos , turnFlag);
  while(gameFlag)
  {
    gameFlag = turn(kugelPos , turnFlag);
  }

  if(turnFlag)
    cout << "Computer gewinnt!" << endl << endl;
  else
    cout << "Spieler gewinnt!" << endl << endl;

  return 0;
}

void newGame(int &kugelPos , bool &turnFlag)
{
  int i;
  srand(time(NULL));

  kugelPos   = (rand() % 6)+1;
  i          = (rand() % 2)+1;

  turnFlag = (i==1) ? false : true;

  system("clear");

  if(turnFlag)
    cout << "Sie beginnen!" << endl << endl;
  else
    cout << "Der Computer beginnt" << endl << endl;

  cout << "Weiter mit <ENTER>";
  getchar();
}

bool shoot(int &kugelPos , bool &turnFlag) // true=Kugel wird geschossen
{
  cout << "Die Waffe wird an die Stirn gelegt und..." << endl;

  if(kugelPos == 1)
  {
    cout << "*PENG*" << endl;
    return true;
  } else
  {
    cout << "*klick*" << endl;
    return false;
  }
}

void spin(int &kugelPos)
{
  int i = rand() % 10 + 1;

  if(i > 6)
    i -= 6;

  kugelPos += i;

  if(kugelPos > 6)
    kugelPos -= 6;
 }

bool turn(int &kugelPos , bool &turnFlag)
{
  int i;
  static int round=1;
  string str;

  str = (turnFlag) ? "Spieler" : "Computer";

  system("clear");

  cout << "An der Reihe:\t" << str << endl
       << "Runde:\t" << round++ << endl
       << "-------------------------------------" << endl;

  if(turnFlag)
  {
    cout << "1 = Schiessen" << endl
         << "2 = Trommel Drehen" << endl;

    cin.sync();
    cin  >> i;

  } else
  {

    i = (rand() % 2)+1;
  }

  switch(i)
  {
    case(1):

      if(shoot(kugelPos , turnFlag))
      {
	return false;
      } else
      {
	getchar();
	if(turnFlag)
	  getchar();

	kugelPos++;
	if(kugelPos > 6)
	  kugelPos -= 6;

	turnFlag = (turnFlag) ? false : true;

	return true;
      }
      break;

    case(2):
      cout << "Das Kugellager wird gedreht...." << endl;
      spin(kugelPos);

      getchar();
      if(turnFlag)
	getchar();

      if(shoot(kugelPos , turnFlag))
      {
	return false;
      } else
      {
	getchar();
	kugelPos++;
	turnFlag = (turnFlag) ? false : true;

	return true;
      }
      break;

    default:
      cout << "Ungueltige Auswahl" << endl;
      getchar();
      return true;
      break;
  }
}
 
Hallo,
naja auch wenn man es in C machen sollte, hier mal der Code für Visual Basic:

Also es gibt 3 Buttons:
cmdDreh
cmdKugel
cmdNeu

Code:
Dim Loch As Integer
Dim Kugel As Integer



Private Sub cmdDreh_Click()
Randomize

Loch = Int((6 * Rnd) + 1)


End Sub

Private Sub cmdNeu_Click()
Loch = Int((6 * Rnd) + 1)
Kugel = 1

cmdShot.Enabled = True
cmdDreh.Enabled = True
cmdNeu.Enabled = False

End Sub

Private Sub cmdShot_Click()

If Loch = Kugel Then
    MsgBox "*Boom*, du bist tot"
    cmdShot.Enabled = False
    cmdDreh.Enabled = False
    cmdNeu.Enabled = True
Else
    MsgBox "*Klack*, Glück gehabt"
End If

Kugel = Kugel + 1

If Kugel > 6 Then
    Kugel = 1
End If

If Loch = Kugel Then
    MsgBox "*Boom*, das hat dein PC nicht überlebt"
    
    cmdShot.Enabled = False
    cmdDreh.Enabled = False
    cmdNeu.Enabled = True
Else
    MsgBox "*Klack*, dein PC lebt noch"
End If

End Sub

Private Sub Form_Load()
Randomize

Loch = Int((6 * Rnd) + 1)
Kugel = 1

End Sub
 
hallo zusammen,

ich habe oben ja schon eine Lösung in C gepostet.

Hier noch eine Lösung in c++ unter Verwendung einer Klasse (CWaffe).

Es wäre dadurch grundsätzlich möglich, das Spiel so zu erweitern, dass
a) mehrere Spieler mit jeweils einer eigenen Instanz von CWaffe spielen (jeder verwendet seinen eigenen Revolver)
b) der Computer einen eigenen Revolver verwendet.

In der ersten Lösung hatte ich bei jedem "Feuern" lediglich die Wahrscheinlichkeit dekrementiert , mit der ein Treffer erzielt wird
Bei jedem "Drehen" hette ich die Wahrscheinlichkeit wieder auf 1/6 hochgesetzt.

Diesmal habe ich ein Objekt erzeugt, bei dem bei jedem Drehen die Position der Kugel festgelegt wird.
Bei jedem Feuern, wird die Kugelposition dekrementiert.
In den Methoden habe ich jeweils Anweisungen eingefügt, die die aktuelle Kugelposition ausgeben.
Dies dient natürlich nur der Analyse und sollte beim Spielen auskommentiert werden.

Interessanterweise benötigt nur eine Methode einen Rückgabewert, nämlich die Methode feuern():
Wenn man ein bisschen darüber nachdenkt, ist es aber logisch.

Hier also meine Dateien:

classes/rr.h:
Code:
#include"CWaffe.h"

class CWaffe; 
bool wahl();
classes/CWaffe.h:
Code:
class CWaffe
{
    public: 
        CWaffe();
        // Methoden
        void vorbereiten();
        void drehen();
        bool feuern();
    private:
        // Variablen
        int position;
};
classes/wahl.cpp:
Code:
#include<iostream>
using namespace std;

bool wahl()
{
    bool wahl;
    cout << "\tdrehen[0] oder feuern[1]" << endl;
    cin >> wahl;
    return wahl;
}
classes/CWaffe.cpp
Code:
#include<iostream>
#include"CWaffe.h"
using namespace std;
CWaffe::CWaffe()
{
    vorbereiten();
};
void CWaffe::vorbereiten()
{
    position = random()%6;
    cout << "\t\t\t\t\tPosition nach dem Vorbereiten: " << position << endl;
};
void CWaffe::drehen()
{
    position = random()%6;
    cout << "\t\t\t\t\tPosition nach dem Drehen: " << position << endl;
    CWaffe::feuern();
};
bool CWaffe::feuern()
{
    if(position)
    {
        cout << "\t\t\t\t\tPosition vor dem Feuern: " << position << endl;
        position--;
        cout << "\t\t\t\t\tPosition nach dem Feuern: " << position << endl;
        return 1;
    }
    else    
    {
        return 0;
    }
};
main.cpp:
Code:
#include<iostream>
#include"classes/rr.h"
using namespace std;
int main()
{
    srandom(time(NULL));
    CWaffe* dieWaffe;
    dieWaffe = new CWaffe; 
    do
    {
        // Spieler ist dran
        if (wahl()) // gleich feuern, wenn wahl !=0 sonst drehen
        {       
            cout << "\tSpieler feuert!" << endl;
            if(dieWaffe->feuern())
                cout << "\tGlueck gehabt!" << endl;
            else    
            {       
                cout << "\tPAENG! \t\tSpieler verliert\n\tGame Over" << endl;
                return 0;
            }       
        }       
        else    
        {       
            cout << "\tSpieler dreht!" << endl;
            dieWaffe->drehen();
        }       
        // Computer ist dran
        if(random()%2)
        {       
            cout << "\tComputer feuert!" << endl;
            if(dieWaffe->feuern())
                cout << "\tGlueck gehabt!" << endl;
            else    
            {       
                cout << "\tPAENG! \t\tComputer verliert\n\tGame Over" << endl;
                return 0;
            }       
        }       
        else    
        {       
            cout << "\tComputer dreht!" << endl;
            dieWaffe->drehen();
        }       
    }
    while(1);
}

Zum Thema Kiste dumm (dreht zufällig oder nach 5 Schuss auf jeden Fall) oder nicht:
Die Option zum Drehen ist eigentlich grundsätzlich unrealistisch, da man vom logischen Standpunkt aus vor jedem Schuß drehen sollte, da hier die Chancen immer höher sind als beim sofortigen Feuern.
 
Hallo zusammen!
Habe heute ebenfalls schnell ein Russisches Roulett geschrieben. Hier der Code:

Code:
// Russisches Roulette by Keymaker 02.05.2004
#include<stdio.h>
#include<conio.h>
#include<time.h>
#include<stdlib.h>

#define TRUE 1
#define FALSE 0

int ausgabe(int kugelort, int position)
{
	printf("Kugelort: %d\n",kugelort);
	printf("Position: %d\n",position);
}

int drehen(int kugelort,int position, int i)
{	
	if(position==kugelort)
	{
		if(i%2==0) printf("\tDer Spieler ist gestorben....\n");
		else printf("\tDer Computer ist gestorben....\n");
		//ausgabe(kugelort,position);
		getch();
		exit(EXIT_SUCCESS);
	}
	else 
	{
		printf("\tGlueck gehabt!!\n\n\n");
		//ausgabe(kugelort,position);
	}
}
int abdruecken(int kugelort, int position, int i)
{
	if(kugelort==position)
	{
		if(i%2==0) printf("\tDer Spieler ist gestorben....\n");
		else printf("\tDer Computer ist gestorben....\n");
		//ausgabe(kugelort,position);
		getch();
		exit(EXIT_SUCCESS);
	}
	else
	{
		printf("\tGlueck gehabt!!\n\n\n");
		//ausgabe(kugelort,position);
	}
}

int main()
{
	int i=1,kugelort=0,wahl=0,position=1,spiel=TRUE,j=0;

	printf("WELCOME TO RUSSISH ROULETT\n    by Keymaker\n\n\n");
	srand( (unsigned)time(NULL) );

	kugelort=rand()%6+1; //Kugelort wird bestimmt
	position=1;
	while(1)
	{
		if(i%2==0) 
		{
			printf("Spieler ist am Ball\n");
			printf("Bitte wählen:\n1. abdruecken\n2. Drehen\nWahl:");
			scanf("%d",&wahl);
		}
		else 
		{
			printf("Computer ist am Ball\n");
			wahl=rand()%2+1;
			//printf("Der Computer hat gewählt: %d\n",wahl);
		}
		

		switch(wahl)
		{
		case 1:
			if(position==6)
				position=1;
			else
				position++;
			abdruecken(kugelort, position, i);
			break;
		case 2:
			position=rand()%6+1;
			drehen(kugelort,position, i);
			break;
		default:
			printf("Keine gültige Wahl\n");
			getch();
			exit(EXIT_FAILURE);
			break;
		}
		system("CLS");
		i++;
	}
	getch();
}
 
Habs jetzt mal nur überflogen, aber Ich glaube Dein Revolver hat nur 5 Kammern :D

Code:
kugelort=rand()%5+1
..
position=rand()%5+1
...
if(position==6)

Hier versteh ich auf die Schnelle nicht warum du poition und kugelort benötigst.
Außerdem wird position wohl Werte zwischen 1 und 5 annehmen und darum wohl nie sechs werden.
Oder hab ich jetzt was übersehen?
Irgendwie finde ich bei Dir den Faden jetzt nicht gleich (soll aber keine Kritik sein)
 
der kugelort gibt an, wo sich die kugel befindet! die position gibt an, wo sich gerade die Trommel befindet bzw welche Trommelkammer gerade an "abschusspostition" steht!
genau... das ist falsch! es müsste sein: rand()%6 habe ich gerade ebenfalls bermerkt!!
danke :)!
 
So, hier ist meine Lösung:

Code:
#include<stdio.h>
#include<conio.h>
#include<time.h>
#include<stdlib.h>

int main()
{
  int kugelort = 1;
  int position = 1;
  int com;
  int a;

  printf ("Willkommen beim Russischen Roulett!              by Ronin\n\n");

  srand ( (unsigned)time(NULL) );
  position=rand()%6+1;

  pla:

  printf ("Was wollen sie tun?\n");
  printf ("1 - Trommel drehen\n");
  printf ("2 - Schiessen");
  scanf  ("%d", &a);

  switch (a)
    {
    case 1:
        srand ( (unsigned)time(NULL) );
        position=rand()%6+1;
        printf ("\nSie drehen die Trommel\n\n");
        goto pla;
        break;
    case 2:
        if ( kugelort == position)
         {
          printf ("\nSie sind Tod!!!\n\n");
          goto ende;
         }
        else
        {
         printf ("\nSie haben Glueck gehabt!\n");
         if ( position == 6)
          {position = 1;}
         else
          {position = position + 1;}
        }
     default:
        break;
    }

   com:

   srand ( (unsigned)time(NULL) );
   com=rand()%2+1;

   switch (com)
    {
      case 1:
        srand ( (unsigned)time(NULL) );
        position=rand()%6+1;
        printf ("\nIhr Gegner dreht vorher die Trommel.\n");
        system ("pause");
      case 2:
        if ( kugelort == position)
         {
          printf ("\nIhr gegner schiesst,\nund ist Tod ...\n");
          goto ende;
         }
        else
        {
         printf ("\nIhr Gegner schiesst,\nund hat Glueck gehabt!\n");
         printf ("\nSie sind dran ...\n");
         if ( position == 6)
          {position = 1;}
         else
          {position = position +1;}
        }
     default:
       break;
     }

  goto pla;

  ende:

  system ("pause");
  return 0;
}



ist denke ich mal nicht ganz so elegant wie eure, aber ich bin ja noch blutiger anfänger ^^
würde mich über Kritik freuen
 
Jo, werde ich mal umrschreiben!
Weiß das die nicht so gerne gesehen werde, aber ich wollte zumindest erstmal nen laufendes Programm schreiben, so als erfolgserlebnis :D
 
halo Ronin,

ich versteh´ nicht ganz, wozu die zwei Variablen "position" und "kugelort" notwendig sind.
Man kann doch eine "position" (bei Dir "1") als "Abschuss"position definieren und dann mit if prüfen ob position == 1.
Die Variable "kugelort" ändert sich ja auch nie.
 
jo, haste recht!
dachte mir das das realistischer sei (wenn es das bei dem programm gibt :D)

weil man ja in echt auch nicht immer in den selben schacht tuen würde,
aber hast recht, werde ich auch noch umschreiben

mfg ronin
 
Original von Ronin
jo, haste recht!
dachte mir das das realistischer sei (wenn es das bei dem programm gibt :D)

weil man ja in echt auch nicht immer in den selben schacht tuen würde,
aber hast recht, werde ich auch noch umschreiben

mfg ronin
unter diesem Aspekt (realistischer) hab ich das gar nicht betrachtet.
Meine Überlegung war, dass eine Variable Speicherplatz belegt - ist bei diesem Miniprogramm zwar egal, aber vom Prinzip her würde ich nur Variablen definieren, die benötigt werden.

Vom logischen Standpunkt aus betrachtet benötigt man eigentlich gar keine Variable, die die "Position" der Kugel beschreibt, sondern nur ein um 1/6 veringerte Wahrscheinlichkeit bei jeder Runde - allerdings muss man da auch mit einer Variablen arbeiten.
 
Und hier eine simple Version in JAVA
Code:
package Hackerboard;
import java.io.*;


/**
 * @author Prussak
 *
 * Russian roulette written in JAVA 
 * 
 * (c) 2004-06-04
 */
public class Version_1 {

    /**
     * method gets a random number between 0 and iBase
     * 
     * @param iBase
     * @return
     */
    private static int randomPositionMod(int iBase){
        return ((int) (Math.random() *100)) % iBase;
    }

    /**
     * method sends true, if iBulletPos and iDrumPos are the same and
     * the revolver is fired
     * 
     * @param iBulletPos
     * @param iDrumPos
     * @return
     */
    private static boolean bulletPosition (int iBulletPos, int iDrumPos){
        return iBulletPos==iDrumPos;
    }

    /**
     * main method
     * 
     * @param args
     */
    public static void main(String[] args) {
        int iBulletPosition = randomPositionMod(6); // initialize the drum with one bullet
        int iDrumPosition   = randomPositionMod(6); // turn drum to an random position
        int iPlayer         = randomPositionMod(2); // throw lot who has the thirst turn
        int iInKey;                                 // only used to fetch the keyboard entries from user
        boolean bDeath;                             // true, if revolver has fired
        
        do{
            /**
             * PC's turn?
             */
            if (iPlayer==0){
                System.out.print("I ");
                if (randomPositionMod(2)==1){  // decide between shoot or turn
                    iDrumPosition = randomPositionMod(6);
                    System.out.print("turned the drum and ");
                }
                System.out.println("shot.");
            }
            /**
             * user's turn
             */
            else{
                System.out.print("Your turn (S)hoot or (T)urn bullet drum ");
                try{
                    iInKey = System.in.read();                           // fetch the key
                    System.in.skip(System.in.available());               // skip all other keys to clean input buffer
                    if (((char)iInKey == 'T') || ((char)iInKey == 't')){ // it doesn't if lower or upper case
                        iDrumPosition = randomPositionMod(6);            // get new random drum position
                    }
                }
                catch(IOException IOError){
                    System.out.println("Some error occured, try again.");
                }
                
            }
            bDeath = bulletPosition (iBulletPosition, iDrumPosition);
            /**
             * revolver hasn't fired?
             */
            if (!bDeath){
                iDrumPosition++; // turn drum to next position
                iPlayer++;       // switch zu next user
                iPlayer%=2;
            }
        }while (!bDeath);
        /**
         * revolver has fired
         */
        System.out.println("\n\n\tB a n g\n\n");
        if (iPlayer==0){
            System.out.println("You'll need a new PC");
        }
        else{
            System.out.println("I'll need a new User");
        }
    }
}
 
unter diesem Aspekt (realistischer) hab ich das gar nicht betrachtet.
Meine Überlegung war, dass eine Variable Speicherplatz belegt - ist bei diesem Miniprogramm zwar egal, aber vom Prinzip her würde ich nur Variablen definieren, die benötigt werden.

Vom logischen Standpunkt aus betrachtet benötigt man eigentlich gar keine Variable, die die "Position" der Kugel beschreibt, sondern nur ein um 1/6 veringerte Wahrscheinlichkeit bei jeder Runde - allerdings muss man da auch mit einer Variablen arbeiten.

hmmmm ... haste auch recht, wenn ich vernünftig programmieren will, dann sollte ich ja schon die größtmögliche effiziens in meinen programmen anstreben,
werde das auf jeden fall bei meinem nächsten programm machen. :)

thx
 
Also, ich hab jetzt mal auch was geschrieben.
Bin immer für Kritik offen. (Programmier noch net arg lange C++)

Code:
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <conio.h>
#include <stdlib.h>
#include <ctime>
using namespace std;



int main(int argc, char *argv[])
{

int player, pc, pkugel, poeffnung;
int eing, zufalleing;
bool i;

i = true;

poeffnung = 1;
player = 1;
pc = 1;

srand(time(NULL));
pkugel = rand() % 6 + 1;

anfang:
for (; i == true;)
{

i = true;

again:
cout << "Du bist an der Reihe!" << endl;
cout << "Was willst du tun?" << endl;
cout << "1. Trommel drehen und schiessen." << endl;
cout << "2. Direkt schiessen." << endl;
cout << endl;
cin >> eing;

if (eing == 1)
{
  cout << "Die Trommel wird gedreht. Und du schiesst danach." << endl;
  
  srand(time(NULL));
  pkugel = rand() % 6 + 1;
}

if (eing == 2)
{ 
  cout << "Du schiesst direkt." << endl;
}

if (eing > 2)
{
  cout << "Bitte 1 oder zwei eingeben" << endl;
  goto again;
}

if (eing < 1)
{
  cout << "Bitte 1 oder zwei eingeben" << endl;
  goto again;
}



if (pkugel == poeffnung)
{
  i = false;
  cout << "Du hast dich leider selbst erschossen! Pech gehabt!" << endl;
  cout << endl;
  goto anfang;
}

else
{
  cout << "Du hast Glueck gehabt. Die Kugel wurde nicht geschossen." << endl;
  cout << endl;
}




cout << endl;
cout << endl;
cout << "Jetzt schiesst der Computer!" << endl;   //Pc ist an der Reihe!

srand(time(NULL));
zufalleing = rand() % 2 + 1;


if (zufalleing == 1)
{
  cout << "Die Trommel wird gedreht. Und der Pc schiesst danach." << endl;
  
  srand(time(NULL));
  pkugel = rand() % 6 + 1;
}

if (zufalleing == 2)
{ 
  cout << "Der Pc schiesst direkt." << endl;
}





if (pkugel == poeffnung)
{
  i = false;
  cout << "Der PC hat sich leider selbst erschossen! Pech gehabt!" << endl;
  cout << "Du hast gewonnen!" << endl;
  cout << endl;
  goto anfang;
}

else
{
  cout << "Der PC hat Glueck gehabt. Die Kugel wurde nicht geschossen." << endl;
  cout << endl;
}
}

cout << endl;
cout << "Danke fuers Spielen. Bitte spiele bald wieder." << endl;
cout << endl;

system("PAUSE");
}



Und da is noch das Programm reingepackt.

Gruß, D31~$0u1

Editiert von CDW, Grund: Code-Tags
 
Hi hackers, hier meine Lösung mit Groovy und einbisschen OO :).
Code:
class Gun 
{
    Random rand = new Random()
    Integer bullet = rand.nextInt(6)
    
    void turn() {
        print " TURN ..."
        bullet = rand.nextInt(6)
    }
    
    void shoot(Player player) {
        println " SHOOT!"
        if (bullet == 0) {
            player.die()
        } else {
            bullet++
            bullet %= 6
        }
    }
}

abstract class Player
{
    static Gun gun = new Gun()
    
    abstract void play();
    abstract void die();
}

class Computer extends Player
{
    void play() {
        print "Computer has the gun ..."
        
        // computer turns the gun 3 of 4 times
        if (gun.rand.nextInt(4) != 0)
            gun.turn()
        gun.shoot(this)
    }
    
    void die() {
        throw new IllegalStateException("Computer shoot himself!")
    }
}

class Human extends Player
{
    void play() {
        print "Human has the gun    ..."
        // human turns the gun 2 of 3 times
        if (gun.rand.nextInt(3) != 0)
            gun.turn()
        gun.shoot(this)
    }

    void die() {
        throw new IllegalStateException("Human shoot himself!")
    }
}

players = [new Computer(), new Human()]
try {
    while (true)
        players.each { it.play() }
} catch (IllegalStateException e) {
    print e.message
}

Und die Ausgabe ...

Code:
Computer has the gun ... TURN ... SHOOT!
Human has the gun    ... TURN ... SHOOT!
Computer has the gun ... TURN ... SHOOT!
Human has the gun    ... SHOOT!
Computer has the gun ... TURN ... SHOOT!
Human has the gun    ... SHOOT!
Human shoot himself!

Gruß, Ivan
 
Nachdem ich nichts besseres zu tun habe, hab ich das mal gecodet...
Code:
// Russisches Roulett von Maurer Christoph

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;


int main() 
{
    srand(time(NULL));
    int patrone = rand()%7; // in welchem schacht ist die patrone (6 schächte)
    char choice;
    int last=0;
    while(1)
    {
            // Sie sind drann
            cout<<"sofort abdruecken?(j/n)";
            cin>>choice;
            if(choice=='j')
            {
                     if(++last==patrone)
                     {
                                        cout<<"Sie sind tod"<<endl;
                                        break;
                     }      
            }
            else
            {
                last = rand()%7;
                if(last==patrone)
                {
                                 cout<<"Sie sind tod"<<endl;
                                 break;
                }
            }
            cout<<"Knack Glueck gehabt!"<<endl;
            // Der Computer ist drann
            last = rand()%7;
            if(last==patrone)
            {
                             cout<<"Der Computer hat sich erschossen"<<endl;
                             break;
            }
            cout<<"Klick... Der Computer lebt auch noch!"<<endl;
    }

  system("PAUSE");
  return 0;
}
 
so hier ist mein geistiger Erguss :D

Code:
//created by Cpt.Flokati

#include <iostream>
#include <cmath>
#include <string>


using namespace std;

bool magazine[5];

void shuffle(){
magazine[0] = false;
magazine[1] = false;
magazine[2] = false;
magazine[3] = false;
magazine[4] = false;
magazine[5] = false;
srand(time(NULL));
int random = rand()%5;
magazine[random] = true;
};
void step() {
bool helper = magazine[0];
magazine[0] = magazine[1];
magazine[1] = magazine[2];
magazine[2] = magazine[3];
magazine[3] = magazine[4];
magazine[4] = magazine[5];
magazine[5] = helper;
};
void neighbour() {
cout<<"its your neighbours turn.."<<endl;
srand(time(NULL));
int temp = rand()%1;
if (temp == 0) {
cout<<"*brrr* your neighbour twisted the magazine"<<endl;}
else{ cout<<"your neighbour pulls the trigger"<<endl;}

if (magazine[0] == true) {
cout<<"your neighbour shot himself. congratulations!!"<<endl;
exit(0);} else {
cout<<"*clack* your neighbour is still alive."<<endl;
step(); }

}


int main()
{

cout <<":::::::::::::::::::russian roulette::::::::::::::::::" << endl;
cout <<"its your turn:"<<endl;
cout <<"type 't' to shuffle the magazine and shoot"<<endl;
cout <<"type 's' to pull the trigger"<<endl;
cout <<"type 'exit' leave the match"<<endl;
cout <<"n"<<endl;
bool shot = false;
shuffle();

string input;


while (!shot) {
cin>>input;
if (input == "s") {
if (magazine[0] == true) {
cout<<"you shot yourself"<<endl;
cout<<"rest in peace."<<endl;
exit(0);
}else {
cout<<"*clack*!. your are still alive."<<endl;

step();
neighbour();
continue;
}
}
else if (input == "t"){
shuffle();
if (magazine[0] == true) {
cout<<"you shot yourself"<<endl;
cout<<"bye."<<endl;
exit(0);
}else {
cout<<"*clack*!. you are still alive."<<endl;

step();
neighbour();
continue;
}
}
else if (input == "exit") {
cout<< "you left the match"<<endl;
exit(0);
} else {continue;};


};


};
 
Zurück
Oben