C Language Mini-game

Source: Internet
Author: User
Tags rand

I. Guess the number game (binary find) control the number of game guesses, each game not more than 10 times    situation one. Guess the right result in a specific requirement Scenario Two: No correct results were guessed within the requirements
#include <stdio.h> #include <stdlib.h> #include <time.h>int main () {int i=1;int input=0;int count=0;   Controls the number of game executions int Random=0;while (i) {printf ("******** Welcome to guessing number game *********\n");p rintf ("Please enter your selection: 1. Start the game 0. Exit the game \ n"); scanf ( "%d", &i), switch (i) {case 1:srand ((unsigned int) time (NULL)), Random=rand ()%101;count=0;while (Count < 5) {printf ("Please enter a number you guessed between 1-100: \ n"); scanf ("%d", &input), if (input = = random) {printf ("Congratulations, guess right \ n"); else if (input > Random) {count++;p rintf ("Guess big \ n");} else{count++;p rintf ("Guess small");}} if (count = = 5) {printf ("Your opportunity is exhausted, the game is over \ n");} Break;case 0:break;default:printf ("Your choice error \ n"); break;}} System ("pause"); return 0;}


Game Two. Three sub-chess games, similar implementations see the following link:http://blog.csdn.net/qq_34328833/article/details/51025652
the difference is that the implementation of the game does not use SZ to determine whether the board is empty (SZ is the beginning of the number of board empty position, regardless of the player or computer as long as one party occupies the position of the board, Sz minus 1, until the SZ Zero stop game), this time used a judge whether the board is empty function, And the implementation of this security is strong, flexibility is also high. situation one. Player wins         scenario two. Computer wins        scenario three. Draw        game.h       
#define _crt_secure_no_warnings 1#ifndef __game_h__   //prevents header files from being repeatedly defined in # define __game_h__#include<stdio.h># include<stdlib.h> #include <time.h> #define ROW 3#define coiumn 3void menu (); void Init_board (char arr[row][ Coiumn]), void Print_board (char arr[row][coiumn]), void Player (char arr[row][coiumn]), int judgefull (char arr[row][ Coiumn]), void computer (char arr[row][coiumn]), Char Winner (char arr[row][coiumn]); #endif//__game_h__

game.c     
#define _crt_secure_no_warnings 1#include "game.h" void menu () {printf ("************* Welcome to the Tri-chess game *************\n"); printf ("* * * * * Please enter your choice 1. Enter the game 0. Exit the game *****\n"); void Init_board (char arr[row][coiumn]) {int i=0;int j=0;for (i=0;i<row;i++) {for (j=0;j<coiumn;j++) {arr[i][j]= ' ;}}} void Print_board (char arr[row][coiumn]) {int i=0;for (i=0;i<row;i++) {printf ("%c | %c | %c \ n ", arr[i][0],arr[i][1],arr[i][2]); if (i! = ROW-1) {printf ("---|---|---\ n ");}} void Player (char arr[row][coiumn]) {int x=0;int y=0;while (1) {printf ("Please enter the coordinates you want to go >\n"); scanf ("%d%d", &x,&y); X--;y--;if (arr[x][y] = = ") {arr[x][y]= ' @ '; break;} else{printf ("Your input error, please re-enter \ n"); continue;}}} int Judgefull (char arr[row][coiumn]) {int i=0;int j=0;for (i=0;i<row;i++) {for (j=0;j<coiumn;j++) {if (arr[i][j] = = ") return 0;}} return-1;} void computer (char arr[row][coiumn]) {int x=0;int y=0;srand ((unsigned int) time (NULL)), while (1) {X=rand ()%row;y=rand () %coiumn;if (arr[x][y] = = ") {arr[x][y]= ' # '; break;}   Else{if (Judgefull (arr) = = 0) continue; Board is not full BReak; Board full}}}char Winner (char arr[row][coiumn]) {int i=0;for (i=0;i<row;i++) {if (arr[i][0] = = Arr[i][1] && arr[i] [1] = = Arr[i][2] && arr[i][0]! = ") return arr[i][0];} for (i=0;i<coiumn;i++) {if (arr[0][i] = = Arr[1][i] && arr[1][i] = = Arr[2][i] && arr[0][i]! = ") return AR R[0][i];} if (arr[0][0] = = Arr[1][1] && arr[1][1] = arr[2][2] && arr[0][0]! = ") {return arr[0][0];} if (arr[0][2] = = Arr[1][1] && arr[1][1] = arr[2][0] && arr[0][2]! = ") {return arr[0][2];} if (Judgefull (arr) = =-1) return-1;       The board is full return 0; Not full won}

test.c   
#define _crt_secure_no_warnings 1#include "game.h" void text () {char ret=0;int Input=1;char arr[row][coiumn]={0};while ( Input) {menu (); scanf ("%d", &input); switch (input) {case 1:init_board (arr); Print_board (arr);d O{player (arr); Computer (arr); Print_board (arr); Ret=winner (arr);} while (ret = = 0); if (ret = = ' @ ') {printf ("Congratulations on winning the championship \");} else if (ret = = ' # ') {printf ("Sorry the computer won \ \");} if (ret = =-1) {printf ("Draw \ n");} Break;case 0:break;default:printf ("Your input error, please re-enter \ n"); int main () {text (); system ("pause"); return 0;}

These two games are repeatable play, I hope the players have fun

C Language Mini-game

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.