Programalama > C++

Etiketler: memory-oyunu

Ort. 0
Puan ver:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define FALSE 0
#define TRUE 1

#define REMOVED 10

void
swap(char *a, char *b)
{
  char t = *a;
  *a = *b;
  *b = t;
}

void 
init(char field[], int *pairsLeft, int *player)
{
  int i, random;

  srand((unsigned int) time(NULL));


  for (i = 0; i < 10; ++i) {
    field[2*i] = field[2*i+1] = i;
  }

 
  for (i = 0; i < 20; ++i) {
    random = (int) ((20.0 - i) * (rand() / (RAND_MAX + 1.0)));
    swap(field + i, field + i + random);
  }

  
  *pairsLeft = 10;
 
  *player = 0;
}


void
displayField(char field[], int turned, int turnedIndex[])
{
  int turnThis = FALSE;
  int i, j, k;

  
  for (i = 0; i < 6; ++i) {
    printf("\n\n\n\n\n\n\n\n\n\n");
  }


  printf("     0   1   2   3   4\n");
  printf("   +---+---+---+---+---+\n");
  for (i = 0; i < 4; ++i) {
   
    printf(" %d |", i);
    for (j = 0; j < 5; ++j) {
      if (*field != REMOVED) {

	turnThis = FALSE;
	for (k = 0; k < turned; ++k) {
	  if ((i == turnedIndex[2*k]) && (j == turnedIndex[2*k+1])) {
	   
	    turnThis = TRUE;
	  
	    break;
	  }
	}
	if (turnThis == TRUE) {
	
	  printf(" %d |", *field);
	} else {
	  
	  printf(" # |");
	}
      } else {

      }
      ++field;
    }
  
    printf("\n");
    printf("   +---+---+---+---+---+\n");
  }
  printf("\n");
}

int 
isMoveOk(int row, int col, int read, 
	 char field[], int turned, int turnedIndex[]) 
{
  int i;

  if (read != 2) {
  
    scanf("%*s");
    printf("Bitte Geben Sie zwei Zahlen wie \"1,2\"\n");
    return FALSE;
  } 
  if ((row < 0) || (row > 3)) {
   
    printf("Zeile ist falsch eingegeben\n");
    return FALSE;
  }
  if ((col < 0) || (col > 4)) {
    
    printf("Spalte ist falsch eingegeben\n");
    return FALSE;
  }
  if (field[row * 5 + col] == REMOVED) {
  
    printf("Card is already removed\n");
    return FALSE;
  }
  for (i = 0; i < turned; ++i) {
    if ((row == turnedIndex[2*i]) && (col == turnedIndex[2*i+1])) {
      
      printf("Card is already turned\n");
      return FALSE;
    }
  }

 
  return TRUE;
}


int
isPair(char field[], int *turned, int *pairsLeft)
{
  if (field[turned[0]*5 + turned[1]] == field[turned[2]*5 + turned[3]]) {
    field[turned[0]*5 + turned[1]] = field[turned[2]*5 + turned[3]] = REMOVED;
    (*pairsLeft)--;
    return TRUE;
  } else {
    return FALSE;
  }
}

int
main(void)
{
  int pairsLeft;                
  int player;                   
  int read;                     
  int turned[4];                
  int playerPairs[2] = {0, 0};  
  char field[20];              
  
  init(field, &pairsLeft, &player);

  while (pairsLeft != 0) {
   
    
    displayField(field, 0, NULL);
   
    printf("Spieler ");
    if (player == 0) {
      printf("1 (%d:%d)\n", playerPairs[0], playerPairs[1]);
    } else {
      printf("2 (%d:%d)\n", playerPairs[1], playerPairs[0]);
    }
   
    do {
      printf("1.Karte (Zeile,Spalte): ");
      read = scanf("%d , %d", turned, turned + 1);
      getchar();
    } while (!isMoveOk(turned[0], turned[1], read, field, 0, NULL));

  
    displayField(field, 1, turned);
   
    printf("Spieler ");
    if (player == 0) {
      printf("1 (%d:%d)\n", playerPairs[0], playerPairs[1]);
    } else {
      printf("2 (%d:%d)\n", playerPairs[1], playerPairs[0]);
    }
 
    do {
      printf("2.Karte (Zeile,Spalte): ");
      read = scanf("%d , %d", turned + 2, turned + 3);
      getchar();
    } while (!isMoveOk(turned[2], turned[3], read, field, 1, turned));

    
    displayField(field, 2, turned);
    if (isPair(field, turned, &pairsLeft)) {
      
      printf("GEWONNEN!\n");
      getchar();
      playerPairs[player]++;
    } else {
     
      printf("nicht gewonnen =(\n");
      getchar();
      if (player == 0) {
	player = 1;
      } else {
	player = 0;
      }
    }
  }

 
  if (playerPairs[0] > playerPairs[1]) {
    printf("Spieler  1 hat gewonnen %d : %d.\n"
	   "Congratulations!!!\n", playerPairs[0], playerPairs[1]);
  } else if (playerPairs[1] > playerPairs[0]) {
    printf("Spieler 2 hat gewonnen %d : %d.\n"
	   "Well done!!!\n", playerPairs[1], playerPairs[0]);
  } else {
    printf("Unendschieden.\n");
  }

  return 0;
}



tarik tekin
xumutxx@hotmail.de


Yorumlar                 Yorum Yaz
Bu hazır kod'a ilk yorumu siz yapın!
KATEGORİLER
ASP - 240
ASP.NET - 24
C# - 75
C++ - 174
CGI - 8
DELPHI - 247
FLASH - 49
HTML - 536
PASCAL - 246
PERL - 11
PHP - 160
WML - 9
XML - 2
Copyright © 2002 - 2024 Hazır Kod - Tüm Hakları Saklıdır.
Siteden yararlanırken gizlilik ilkelerini okumanızı tavsiye ederiz.
hazirkod.com bir İSOBİL projesidir.