Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #define COLS 7
- #define ROWS 6
- int win = 0;
- /*************************************************/
- /* Tomb feltoltese, kiiratasa */
- /*************************************************/
- void feltolt(char feltolt[COLS][ROWS]){
- int i, j;
- for(i = 0; i < ROWS; i++){
- for(j = 0; j < COLS; j++){
- feltolt[j][i] = '-';
- }
- }
- }
- void kiir(char kiirat[COLS][ROWS]){
- int i, j;
- printf("\n\n");
- printf("1 2 3 4 5 6 7\n");
- printf("*************\n");
- for(i = 0; i < ROWS; i++){
- for(j = 0; j < COLS; j++)
- printf("%c ", kiirat[j][i]);
- printf("\n");
- }
- printf("*************\n");
- printf("*************\n");
- }
- /*************************************************/
- /* Player1 valasztasa */
- /*************************************************/
- int bele1(char tomb[COLS][ROWS]){
- char string[255];
- int i, place, bad = 0;
- printf("\nP1: Chose a column: ");
- do{
- if(bad == 0){
- scanf("%s", &string);
- if(!strcmp(string, "feladom")){
- return -1;
- }
- place = atoi(string);
- bad++;
- }
- else{
- printf("\nInvalid move: Chose another column (1-7)");
- scanf("%s", &string);
- place = atoi(string);
- }
- bad++;
- }while(checkifplacable(tomb, place) != 1);
- for(i = ROWS-1; i >= 0; i--){ //Placement
- if(tomb[place-1][i] == '-'){
- tomb[place-1][i] = 'X';
- checkforwin1(tomb);
- break;
- }
- }
- kiir(tomb);
- return 1;
- }
- /*************************************************/
- /* Player2 valasztasa */
- /*************************************************/
- int bele2(char tomb[COLS][ROWS]){
- char string[255];
- int i, place, bad = 0;
- printf("\nP2: Chose a column: ");
- do{
- if(bad == 0){
- scanf("%s", &string);
- if(!strcmp(string, "feladom")){
- return -1;
- }
- place = atoi(string);
- bad++;
- }
- else{
- printf("\nInvalid move: Chose another column (1-7)");
- scanf("%d", &place);
- }
- bad++;
- }while(checkifplacable(tomb, place) != 1);
- for(i = ROWS-1; i >= 0; i--){ //Placement
- if(tomb[place-1][i] == '-'){
- tomb[place-1][i] = 'O';
- checkforwin2(tomb);
- break;
- }
- }
- kiir(tomb);
- return 1;
- }
- /*************************************************/
- /* Checks */
- /*************************************************/
- int checkifplacable(char tomb[COLS][ROWS], int place){
- if(place < 1 || place > 7 || tomb[place-1][0] != '-') return -1;
- else return 1;
- }
- int checkforwin1(char tomb[COLS][ROWS]){
- int i, j, oszlopban = 0;
- for(i = 0; i < COLS; i++){ // OSZLOPBAN
- for(j = 0; j < ROWS-3; j++){
- if(tomb[i][j] == 'X' && tomb[i][j+1] == 'X' && tomb[i][j+2] == 'X' && tomb[i][j+3] == 'X')
- oszlopban = 1;
- }
- }
- for(i = 0; i < COLS-3; i++){ // SORBAN
- for(j=0; j<ROWS; j++){
- if(tomb[i][j] == 'X' && tomb[i+1][j] == 'X' && tomb[i+2][j] == 'X' && tomb[i+3][j] == 'X')
- oszlopban = 1;
- }
- }
- for(i = 0; i < COLS-3; i++){ // ATLOBAN LEFELE
- for(j = 0; j < ROWS-3; j++){
- if(tomb[i][j] == 'X' && tomb[i+1][j+1] == 'X' && tomb[i+2][j+2] == 'X' && tomb[i+3][j+3] == 'X')
- oszlopban = 1;
- }
- }
- for(i = 0; i < COLS-3; i++){ // ATLOBAN FELFELE
- for(j = ROWS; j > 3; j--){
- if(tomb[i][j] == 'X' && tomb[i+1][j-1] == 'X' && tomb[i+2][j-2] == 'X' && tomb[i+3][j-3] == 'X')
- oszlopban = 1;
- }
- }
- win = oszlopban;
- //return oszlopban;
- }
- int checkforwin2(char tomb[COLS][ROWS]){
- int i, j, oszlopban = 0;
- for(i = 0; i < COLS; i++){ // OSZLOPBAN
- for(j = 0; j < ROWS-3; j++){
- if(tomb[i][j] == 'O' && tomb[i][j+1] == 'O' && tomb[i][j+2] == 'O' && tomb[i][j+3] == 'O')
- oszlopban = 1;
- }
- }
- for(i = 0; i < COLS-3; i++){ // SORBAN
- for(j=0; j<ROWS; j++){
- if(tomb[i][j] == 'O' && tomb[i+1][j] == 'O' && tomb[i+2][j] == 'O' && tomb[i+3][j] == 'O')
- oszlopban = 1;
- }
- }
- for(i = 0; i < COLS-3; i++){ // ATLOBAN LEFELE
- for(j = 0; j < ROWS-3; j++){
- if(tomb[i][j] == 'O' && tomb[i+1][j+1] == 'O' && tomb[i+2][j+2] == 'O' && tomb[i+3][j+3] == 'O')
- oszlopban = 1;
- }
- }
- for(i = 0; i < COLS-3; i++){ // ATLOBAN FELFELE
- for(j = ROWS; j >= 3; j--){
- if(tomb[i][j] == 'O' && tomb[i+1][j-1] == 'O' && tomb[i+2][j-2] == 'O' && tomb[i+3][j-3] == 'O')
- oszlopban = 1;
- }
- }
- win = oszlopban;
- //return oszlopban;
- }
- int istheremorespace(char tomb[COLS][ROWS]){
- if(tomb[1][0] != '-' && tomb[2][0] != '-' && tomb[3][0] != '-' && tomb[4][0] != '-' && tomb[5][0] != '-' && tomb[6][0] != '-' && tomb[7][0] != '-'){
- printf("\nTIE\nMatch is over!\tThere is no more free space!");
- return -1;
- }
- return 1;
- }
- /*************************************************/
- /* HUB */
- /*************************************************/
- int hub(){
- win = 0;
- int i, j, oszlop;
- char table[COLS][ROWS];
- feltolt(table);
- kiir(table);
- while(win == 0){
- if((istheremorespace(table)) == -1)
- break;
- if(bele1(table) == 1){
- if(win == 1){
- printf("Player 1 wins the match");
- break;
- }
- }
- else {
- printf("Player 2 wins the match, as Player 1 surrendered!");
- return 0;
- }
- if(bele2(table) == 1){
- if(win == 1){
- printf("Player 2 wins the match");
- break;
- }
- }
- else {
- printf("Player 1 wins the match, as Player 2 surrendered!");
- return 0;
- }
- }
- return 0;
- }
- /*************************************************/
- /* MAIN */
- /*************************************************/
- int main(){
- hub();
- printf("\nDo you want a rematch? (ujra/vege)\t");
- char valasztas1[255];
- char valasztas2[255];
- printf("\nP1: ");
- scanf("%s", valasztas1);
- printf("P2: ");
- scanf("%s", valasztas2);
- if(!strcmp(valasztas1, "vege"))
- printf("\nPlayer1 abandoned the match.\nThe session will close soon.");
- if(!strcmp(valasztas2, "vege"))
- printf("\nPlayer2 abandoned the match.\nThe session will close soon.");
- if(!strcmp(valasztas1, "ujra") && !strcmp(valasztas2, "ujra")){
- printf("\nThe rematch will start soon!");
- hub();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement