c 언어 편찬 삼자기
text.c
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include"game.h"
void game()
{
int ret = 0;
char board[ROWS][COLS]={0};
init_board(board,ROWS,COLS);
display_board(board,ROWS,COLS);
srand((unsigned int)time(NULL));
while(1)
{
player_move(board,ROWS,COLS);
if((ret = check_win(board, ROWS, COLS)) != ' ')
break;
display_board(board,ROWS,COLS);
computer_move(board,ROWS,COLS);
if((ret = check_win(board, ROWS, COLS)) != ' ')
break;
display_board(board,ROWS,COLS);
}
ret = check_win(board, ROWS, COLS);
if(ret == 'X')
{
printf(" ! !
");
}
else if(ret == 'O')
{
printf(" ! !
");
}
else if(ret == 'q')
{
printf(" !
");
}
display_board(board, ROWS, COLS);
}
void menu()
{
printf("*******************************
");
printf("****** 1. play 0. exit ******
");
printf("*******************************
");
}
int main ()
{
int input = 0;
do
{
menu();
scanf("%d",&input);
switch(input)
{
case 1:
game();
break;
case 0:
break;
default:
printf(" !!");
break;
}
}while(input);
return 0;
}
game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void init_board(char board[ROWS][COLS],int row,int col)
{
memset(board,' ',row*col);
}
void display_board(char board[ROWS][COLS],int row,int col)
{
int i = 0;
for(i=0; i<row; i++)
{
printf(" %c | %c | %c
",board[i][0],board[i][1],board[i][2]);
if(i != 2)
printf("---|---|---
");
}
}
void player_move(char board[ROWS][COLS],int row,int col)
{
int x = 0;
int y = 0;
while(1)
{
printf(" :");
scanf("%d%d",&x,&y);
x--;
y--;
if(((x>=0)&&(x<=2))&&((y>=0)&&(y<=2)))
{
if(board[x][y] == ' ')
{
board[x][y]='X';
break;
}
else
{
printf(" !");
}
}
else
{
printf(" !");
}
}
}
void computer_move(char board[ROWS][COLS],int row,int col)
{
printf(" :
");
while(1)
{
int x = rand()%3;
int y = rand()%3;
if(board[x][y] == ' ')
{
board[x][y] = 'O';
break;
}
}
}
static int is_full(char board[ROWS][COLS],int row,int col)
{
int i = 0;
int j = 0;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(board[i][j]==' ')
return 0;
}
}
return 1;
}
char check_win(char board[ROWS][COLS], int row, int col)
{
int i = 0;
for(i=0; i<row; i++)
{
if((board[i][0]==board[i][1])
&&(board[i][1]==board[i][2])
&&(board[i][1]!=' '))
return board[i][1];
}
for(i=0; i<col; i++)
{
if((board[0][i]==board[1][i])
&&(board[1][i]==board[2][i])
&&(board[1][i]!=' '))
return board[1][i];
}
if((board[0][0]==board[1][1])
&&(board[1][1]==board[2][2])
&&(board[1][1]!=' '))
return board[1][1];
if((board[0][2]==board[1][1])
&&(board[1][1]==board[2][0])
&&(board[1][1]!=' '))
return board[1][1];
if(is_full(board, row, col))
{
return 'q';
}
return ' ';
}
game.h
#ifndef __GAME_H__
#define __GAME_H__
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define ROWS 3
#define COLS 3
void init_board(char board[ROWS][COLS],int row,int col);
void display_board(char board[ROWS][COLS],int row,int col);
void player_move(char board[ROWS][COLS],int row,int col);
void computer_move(char board[ROWS][COLS],int row,int col);
char check_win(char board[ROWS][COLS], int row, int col) ;
#endif //__GAME_H__
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.