0% found this document useful (0 votes)
29 views19 pages

Examenesejimprimir

The document describes code for several programs: 1) A snake game where the player moves a snake to eat fruit within a boundary using keyboard keys, increasing the score. When the snake hits the boundary, the game ends. 2) A tic-tac-toe game where two players alternate placing Xs and Os on a 3x3 board until someone wins in a row, column, or diagonal, or there is a draw. 3) A function that generates a magic square of a given odd size by filling it with numbers from 1 to size^2 in a specific pattern. 4) An ATM program that allows a user to check their balance, withdraw or deposit money using a pin number for
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views19 pages

Examenesejimprimir

The document describes code for several programs: 1) A snake game where the player moves a snake to eat fruit within a boundary using keyboard keys, increasing the score. When the snake hits the boundary, the game ends. 2) A tic-tac-toe game where two players alternate placing Xs and Os on a 3x3 board until someone wins in a row, column, or diagonal, or there is a draw. 3) A function that generates a magic square of a given odd size by filling it with numbers from 1 to size^2 in a specific pattern. 4) An ATM program that allows a user to check their balance, withdraw or deposit money using a pin number for
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

//EJERCICIO SERPIENTE

/*The snake is represented by 0.


The fruit is represented by *.
Move the snake in direction with the keys F, D, J, K keys.
With every fruit the snake eats the score increases by 5 points.
Fruits will be generated automatically within the boundary.
The game terminates as the snake collides with the boundary.

Draw() under this function the boundary of game will be specified.


Fruits() which will generate the fruits randomly within the
boundary.
Input() under this function the user input from the keyboard is
taken.
Logic() which contains the logic set of movement of snake in the
game.*/

/*
int i,j;
int height = 40;
int width = 40;

int i, j, height = 20, width = 20;


int gameover, score;
int x, y, fruitx, fruity, flag;

// fruit generation within the boundary

int main(){
// boundary generation
fruits();

// till the game is over


while (!gameover)
{
draw();
input();
logic();
}
}

//POSICION FRUTAS

void fruits(){

gameover = 0;

// store height and width


x = height / 2;
y = width / 2;
fruitsx:
fruitx = rand() % 20;
if (fruitx == 0)
goto fruitsx;
fruitsy:
fruity = rand() % 20;
if (fruity == 0)
goto fruitsy;
score = 0;
}
//DIBUJAR FRONTERAS

void draw(){

for (i = 0; i < height; i++)


{
for (j = 0; j < width; j++)
{
if (i == 0 || i == width - 1 || j == 0 || j == height - 1)
{
printf("#");
}
else
{
if (i == x && j == y)
printf("0");
else if (i == fruitx
&& j == fruity)
printf("*");
else
printf(" ");
}
}
printf("\n");
}
printf("score = %d", score);
printf("\n");
printf("press X to quit the game");
}

void input()
{
if (kbhit())
{
switch (getch())
{
case 'f':
flag = 1;
break;
case 'd':
flag = 2;
break;
case 'j':
flag = 3;
break;
case 'k':
flag = 4;
break;
case 'x':
gameover = 1;
break;
}
}
}

void logic()
{
sleep(1);
switch (flag)
{
case 1:
x++;
break;
case 2:
y--;
break;
case 3:
x--;
break;
case 4:
y++;
break;
default:
break;
}

// game over
if (x < 0 || x > height || y < 0 || y > width)
gameover = 1;
// snake reaches the fruit and the score gets updated
if (x == fruitx && y == fruity)
{
logicx:
fruitx = rand() % 20;
if (fruitx == 0)
goto logicx;

// generation of new fruit after eating the current one


logicy:
fruity = rand() % 20;
if (fruity == 0)
goto logicy;
score += 5;
}
}

//TRES EN RAYA

char board[3][3];

// Function to initialize the game board

void initializeBoard()
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
board[i][j] = ' ';
}
}
int count = 1;
printf("\n\n\t ");
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
printf("%d", count++);
if (j < 2)
{
printf(" | ");
}
}
if (i < 2)
printf("\n\t----------------\n\t ");
}
printf("\n\n\n");
}

// Function shows the game board

void showBoard(int x, int y)


{
printf("\n\n\t ");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%c", board[i][j]);
if (j < 2)
{
printf(" | ");
}
}
if (i < 2)
printf("\n\t----------------\n\t ");
}
printf("\n\n\n");
}

// Function to update the game board

int updateBoard(int cell, char playerSign)


{
int row = (cell - 1) / 3;
int col = (cell - 1) % 3;
int isValid = 1;

// accessing already played cell number


if (board[row][col] != ' ')
{
printf("\nInvalid: Cell is already Filled!\n");
isValid = 0;
}

// valid cell position


else
{
board[row][col] = playerSign;
}
showBoard(row, col);

return isValid;
}

// Function to check the winner of the game

int checkWinner(char sg)


{
// check all rows
if (board[0][0] == sg && board[0][1] == sg && board[0][2] ==
sg ||
board[1][0] == sg && board[1][1] == sg && board[1][2] == sg
||
board[2][0] == sg && board[2][1] == sg && board[2][2] == sg)
{
return 1;
}
// check all columns
else if (board[0][0] == sg && board[1][0] == sg && board[2][0]
== sg ||
board[0][1] == sg && board[1][1] == sg && board[2][1] ==
sg ||
board[0][2] == sg && board[1][2] == sg && board[2][2] ==
sg)
{
return 1;
}
// check both diagonals
else if (board[0][0] == sg && board[1][1] == sg && board[2][2]
== sg ||
board[0][2] == sg && board[1][1] == sg && board[2][0] ==
sg)
{
return 1;
}

// There is no winner
return 0;
}

// Start your game from here


void playTicTacToe()
{
int gameResult = 0;
int cell = 0;
int playCount = 0;
int updationResult = 1;

char playerSign = ' ';

// start playing the game


while (!gameResult && playCount < 9)
{
if (playCount % 2 == 0)
{
// player 1
printf("\nPlayer 1 [ X ] : ");
playerSign = 'X';
}
else
{
// player 2
printf("\nPlayer 2 [ O ] : ");
playerSign = 'O';
}
scanf("%d", &cell);
if (cell > 0 && cell < 10)
{
updationResult = updateBoard(cell, playerSign);
// if updation is possible
if (updationResult)
{
gameResult = checkWinner(playerSign);
// print the winner of the game
if (gameResult)
{
printf("\t *** Player %d Won!! ***\n", playerSign ==
'X' ? 1 : 2);
}
playCount++;
}
}
else if (cell == -1)
{
printf("\n\tGame Terminated\n");
return;
}
else
{
printf("\nPlease Enter a valid cell value\n");
}
}

// no one won the game


if (!gameResult && playCount == 9)
{
printf("\n\t *** Draw... ***\n");
}
printf("\n\t --- Game Over --- \n");
}

int main()
{
printf("--------- Tic Tac Toe ----------\n\n");

printf("\n* Instructions \n\n");


printf("\tPlayer 1 sign = X\n");
printf("\tPlayer 2 sign = O");
printf("\n\tTo exit from game, Enter -1\n");

printf("\n\n* Cell Numbers on Board\n");


initializeBoard();
char start = ' ';
printf("\n> Press Enter to start...");

scanf("%c", &start);

if (start)
{
int userChoice = 1;
// restart the game
while (userChoice)
{
playTicTacToe();
printf("\n* Menu\n");
printf("\nPress 1 to Restart");
printf("\nPress 0 for Exit");
printf("\n\nChoice: ");
scanf("%d", &userChoice);
if (userChoice)
{
initializeBoard();
}
printf("\n");
}
}
printf("\n :: Thanks for playing Tic Tac Toe game! :: \n");
return 0;
}

DADO UNA DIMENSION TE HACE UN CUADRADO


MAGICO
void magicsq(int, int [][10]);

int main( ){
int size;
int a[10][10];

printf("Enter the size: ");


scanf("%d", &size);
if (size % 2 == 0)
{
printf("Magic square works for an odd numbered size\n");
}
else
{
magicsq(size, a);
}
return 0;
}

void magicsq(int size, int a[][10]){


int sqr = size * size;
int i = 0, j = size / 2, k;

for (k = 1; k <= sqr; ++k)


{
a[i][j] = k;
i--;
j++;

if (k % size == 0)
{
i += 2;
--j;
}
else
{
if (j == size)
{
j -= size;
}
else if (i < 0)
{
i += size;
}
}
}
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
printf("%d ", a[i][j]);
}
printf("\n");
}
printf("\n");
}

ATM TRANSACTION

unsigned long amount=1000, deposit, withdraw;


int choice, pin, k;
char transaction ='y';

void main()
{
while (pin != 1520)
{
printf("ENTER YOUR SECRET PIN NUMBER:");
scanf("%d", &pin);
if (pin != 1520)
printf("PLEASE ENTER VALID PASSWORD\n");
}
do
{
printf("********Welcome to ATM Service**************\
n");
printf("1. Check Balance\n");
printf("2. Withdraw Cash\n");
printf("3. Deposit Cash\n");
printf("4. Quit\n");
printf("******************?
**************************?*\n\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("\n YOUR BALANCE IN Rs : %lu ", amount);
break;
case 2:
printf("\n ENTER THE AMOUNT TO WITHDRAW: ");
scanf("%lu", &withdraw);
if (withdraw % 100 != 0)
{
printf("\n PLEASE ENTER THE AMOUNT IN
MULTIPLES OF 100");
}
else if (withdraw >(amount - 500))
{
printf("\n INSUFFICENT BALANCE");
}
else
{
amount = amount - withdraw;
printf("\n\n PLEASE COLLECT CASH");
printf("\n YOUR CURRENT BALANCE IS%lu",
amount);
}
break;
case 3:
printf("\n ENTER THE AMOUNT TO DEPOSIT");
scanf("%lu", &deposit);
amount = amount + deposit;
printf("YOUR BALANCE IS %lu", amount);
break;
case 4:
printf("\n THANK U USING ATM");
break;
default:
printf("\n INVALID CHOICE");
}
printf("\n\n\n DO U WISH TO HAVE ANOTHER
TRANSCATION?(y/n): \n");
fflush(stdin);
scanf("%c", &transaction);
if (transaction == 'n'|| transaction == 'N')
k = 1;
} while (!k);
printf("\n\n THANKS FOR USING OUT ATM SERVICE");
}
JUEGO DEL AHORCADO

char frase[60],rep[100],temporal[100];
char pal;
int longitud,i,j,inicial,acertado=0,temp=0,oportunidades=5;
int repetido=0,gano=0;

printf("\tJuego del Ahorcado\n");


printf("Introduzca la palabra a adivinar: ");
fgets(frase,10,stdin);

longitud = 0;
inicial = 0;
j = 0;

rep[0] = ' ';


rep[1] = '\0';

do {
temp=0;

if(inicial == 0) {
for(i=0;i<strlen(frase);i++) {
if(frase[i] == ' ') {
temporal[i] = ' ';
longitud++;
}
else {
temporal[i] = '_';
longitud++;
}
}
}

inicial = 1;

temporal[longitud] = '\0';

for(i=0;i<strlen(rep);i++) {
if(rep[i] == pal) {
repetido = 1;
break;
}
else {
repetido = 0;
}
}

if(repetido == 0) {
for(i=0;i<strlen(frase);i++) {
if(frase[i] == pal) {
temporal[i] = pal;
acertado++;
temp=1;
}
}
}

if(repetido == 0) {
if(temp == 0) {
oportunidades = oportunidades - 1;
}
}
else {
printf("Ya se ha introducido este caracter");
printf("\n\n");
}

printf("\n");

for(i=0;i<strlen(temporal);i++) {
printf(" %c ",temporal[i]);
}

printf("\n");

if(strcmp(frase,temporal) == 0) {
gano = 1;
break;
}

printf("\n");
printf("Letras Acertadas: %d",acertado);
printf("\n");
printf("Oportunidades Restantes: %d",oportunidades);
printf("\n");

rep[j] = pal;
j++;

if (oportunidades==0)
{
break;
}

printf("Introduzca una letra:");


scanf("\n%c",&pal);

}while(oportunidades != 0);

if(gano) {
printf("\n\n");
printf("Enhorabuena, has ganado.");
}
else {
printf("\n\n");
printf("Has perdido.");
}

printf("\n\n");
return 0;

You might also like