#include <graphics.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
// redni brojevi u tabeli
/*
147
258
369
*/
void draw_table();
void draw_x(int position);
void draw_o(int position);
void draw_test(); // iscrtaj sve X i O
void play(char znak,int igrac);
void play_game();
main ( )
{
initwindow (800,800);/*otvara prozor veli?ine 800 x 600 piksela*/
play_game();
while (!kbhit( )); /*zadržava prozor sa crtežom otvorenim*/
closegraph( );
}
/* Potrebne bibljoteke
-lbgi
-lgdi32
-lcomdlg32
-luuid
-loleaut32
-lole32
*/
void draw_x(int position){
switch(position){
// column I
case 1:line(100,100,300,300);line(300,100,100,300);break;
case 2:line(100,300,300,500);line(300,300,100,500);break;
case 3:line(100,500,300,700);line(300,500,100,700);break;
// column II
case 4:line(300,100,500,300);line(500,100,300,300);break;
case 5:line(300,300,500,500);line(500,300,300,500);break;
case 6:line(300,500,500,700);line(500,500,300,700);break;
// column III
case 7:line(500,100,700,300);line(700,100,500,300);break;
case 8:line(500,300,700,500);line(700,300,500,500);break;
case 9:line(500,500,700,700);line(700,500,500,700);break;
}
}
void draw_o(int position){
switch(position){
// column I
case 1:circle(200,200,100);break;
case 2:circle(200,400,100);break;
case 3:circle(200,600,100);break;
// column II
case 4:circle(400,200,100);break;
case 5:circle(400,400,100);break;
case 6:circle(400,600,100);break;
// column III
case 7:circle(600,200,100);break;
case 8:circle(600,400,100);break;
case 9:circle(600,600,100);break;
}
}
void draw_test(){
draw_x(1);
draw_x(2);
draw_x(3);
draw_x(4);
draw_x(5);
draw_x(6);
draw_x(7);
draw_x(8);
draw_x(9);
draw_o(1);
draw_o(2);
draw_o(3);
draw_o(4);
draw_o(5);
draw_o(6);
draw_o(7);
draw_o(8);
draw_o(9);
}
void play(char znak,int igrac){
if(igrac%2==0){
switch(znak){
case '1':draw_o(1);break;
case '2':draw_o(2);break;
case '3':draw_o(3);break;
case '4':draw_o(4);break;
case '5':draw_o(5);break;
case '6':draw_o(6);break;
case '7':draw_o(7);break;
case '8':draw_o(8);break;
case '9':draw_o(9);break;
}
}
else {
switch(znak){
case '1':draw_x(1);break;
case '2':draw_x(2);break;
case '3':draw_x(3);break;
case '4':draw_x(4);break;
case '5':draw_x(5);break;
case '6':draw_x(6);break;
case '7':draw_x(7);break;
case '8':draw_x(8);break;
case '9':draw_x(9);break;
}
}
}
void draw_table(){
// crtanje pravougaonika oko
rectangle(100,100,700,700);
// vertical
line(300,100,300,700);
line(500,100,500,700);
// horizontal lines
line(100,300,700,300);
line(100,500,700,500);
// dodaj brojeve svakog polja
outtextxy(200,100,"1");
outtextxy(400,100,"4");
outtextxy(600,100,"7");
outtextxy(200,300,"2");
outtextxy(400,300,"5");
outtextxy(600,300,"8");
outtextxy(200,500,"3");
outtextxy(400,500,"6");
outtextxy(600,500,"9");
}
void play_game(){
setfillstyle(SOLID_FILL,BLACK);
bar(0,0,800,800);
draw_table();
settextstyle(1,0,1);
int i=0;
char str[100]="";
char c;
char poruka[100]="";
int igrac=1;
setcolor(RED);
sprintf(poruka,"Sada igra igrac X unesite broj polja 1..9, pritisnite ENTER");
outtextxy(5,5,poruka);
while ((c=getch())!='q'){
if (c!='\r'){
str[i++]=c;
setcolor(WHITE);
outtextxy(40,40,str);
// ako je sve ok igraj
play(c,igrac);
if(c=='r') {
// igraj ponovo igru
setcolor(WHITE);
outtextxy(40,40,"Ponovo igraj");
play_game();
}
}
else {
//clear screen
i=0;
igrac++; // sledeci igrac
setcolor(BLACK);
outtextxy(40,40,str);
for (int j=0;j<99;j++){
str[j]=' ';
poruka[j]=' ';
}
//
setcolor(WHITE);
if(igrac%2==0){
setcolor(YELLOW);
sprintf(poruka,"Sada igra igrac O unesite broj polja 1..9, pritisnite ENTER");
outtextxy(5,5,poruka);
}
else {
setcolor(RED);
sprintf(poruka,"Sada igra igrac X unesite broj polja 1..9, pritisnite ENTER");
outtextxy(5,5,poruka);
}
}
}
exit(0);
}
No comments:
Post a Comment