Program C++

Program Permainan XOX

/////////////////////////////////// created by alfian ansyar //// class TI-3C //// NIM 06.01.029 //// //// With descrpsion ///////////////////////////////////
//Norts & Crosses
// Header Files#include // Used for 'cout' and 'cin' functions.
// Prototypes (each function must have a prototype)void DrawGameBoard(void); // Does not return any value (void); No Paramters (void)void ClearBoard(void); // Does not return any value (void); No Paramters (void)int GameOver(void); // Returns an Integer; No Paramters (void)int GetWinnerInfo(void); // Returns an Integer; No Paramters (void)
// Globals (variables outside the functions are available to all)char gameBoard[9]; // An array of the board in Memory (0 to 8)char gameOverBoard[9]; // An array of the temp board to draw line thru winner.char currentPlayer = 'O'; // Current Player, 'X' or 'O'.char winnerPlayer; // The winners piece, ('X' or 'O') or ' ' if no one won.
int main(){ int inputPos; // Integer variable used by 'cin' to store input of 'X' or 'O' position on the board
cout << "Norts And Crosses\n"; // Prints "Norts And Crosses" to the screen, '\n' prints a newline. // Calls the functions ClearBoard(); DrawGameBoard(); while(1) // 1 is TRUE, so this will loop forever or until 'break' is used. { cout << "\nEnter a number 0-8 or 10 to Exit\n"; cin >> inputPos; // Get input from user and store it in inputPos (an int variable) if ((inputPos >= 0) && (inputPos <= 8)) // If inputPos is between 0 and 8 then... { if (gameBoard[inputPos] == ' ') // If the position on the board is empty then... { gameBoard[inputPos] = currentPlayer; // Set the piece to the currentPlayer ('X' or 'O') at inputPos DrawGameBoard(); // Redraw the board // Swap X and O if (currentPlayer == 'O') currentPlayer = 'X'; else currentPlayer = 'O'; } if (GameOver() == 1) // If GameOver function returns 1 (TRUE) then break; // 'break' out of the loop. } else // else if inputPos = 10 then 'break' out of the loop. (quit game) if (inputPos == 10) break; } cout << "\n\nGAME OVER\n"; if (winnerPlayer != ' ') // If winnerPlayer does NOT = ' ' then... cout << "Player " << winnerPlayer << " Wins!\n\n"; else cout << "Nobody wins.\n\n";
return 0; // main() Should always return 0}
void DrawGameBoard(){ // ___ ___ ___ // // 0 1 2 // _________ // // 3 4 5 // _________ // // 6 7 8 // _________
cout << " ___ ___ ___\n"; cout << " \n"; cout << " " << gameBoard[0] <<" " << gameBoard[1] << " " << gameBoard[2] << " \n"; cout << "_________\n"; cout << " \n"; cout << " " << gameBoard[3] <<" " << gameBoard[4] << " " << gameBoard[5] << " \n"; cout << "_________\n"; cout << " \n"; cout << " " << gameBoard[6] <<" " << gameBoard[7] << " " << gameBoard[8] << " \n"; cout << "_________\n";}
void ClearBoard(){ for (int i = 0; i < 9; i++) { gameBoard[i] = ' '; }}
int GameOver(){ int tmpGameOver; int tmp = 1;
// Check if board is full for (int i = 0; i < 9; i++) { if (gameBoard[i] == ' ') tmp = 0; }
tmpGameOver = GetWinnerInfo(); if ((tmp == 0) && (tmpGameOver == 1)) tmp = 1;
if (tmp == 1) { cout << " ___ ___ ___\n"; cout << " \n"; cout << " " << gameOverBoard[0] <<" " << gameOverBoard[1] << " " << gameOverBoard[2] << " \n"; cout << "_________\n"; cout << " \n"; cout << " " << gameOverBoard[3] <<" " << gameOverBoard[4] << " " << gameOverBoard[5] << " \n"; cout << "_________\n"; cout << " \n"; cout << " " << gameOverBoard[6] <<" " << gameOverBoard[7] << " " << gameOverBoard[8] << " \n"; cout << "_________\n"; }
return tmp;}
// Return 0 if no one has wonint GetWinnerInfo(){ char tmpPlayer = 'O'; int i = 0;
winnerPlayer = ' ';
// reset borad2 for (i = 0; i < 9; i++) { gameOverBoard[i] = gameBoard[i]; }
for (i = 0; i < 2; i++) { if (winnerPlayer != ' ') break; if (i == 1) tmpPlayer = 'X'; if ((gameBoard[0] == tmpPlayer) && (gameBoard[1] == tmpPlayer) && (gameBoard[2] == tmpPlayer)) { gameOverBoard[0] = '-'; gameOverBoard[1] = '-'; gameOverBoard[2] = '-'; winnerPlayer = tmpPlayer; } else{if ((gameBoard[3] == tmpPlayer) && (gameBoard[4] == tmpPlayer) && (gameBoard[5] == tmpPlayer)) { gameOverBoard[3] = '-'; gameOverBoard[4] = '-'; gameOverBoard[5] = '-'; winnerPlayer = tmpPlayer; } else{if ((gameBoard[6] == tmpPlayer) && (gameBoard[7] == tmpPlayer) && (gameBoard[8] == tmpPlayer)) { gameOverBoard[6] = '-'; gameOverBoard[7] = '-'; gameOverBoard[8] = '-'; winnerPlayer = tmpPlayer; } else{if ((gameBoard[0] == tmpPlayer) && (gameBoard[3] == tmpPlayer) && (gameBoard[6] == tmpPlayer)) { gameOverBoard[0] = ''; gameOverBoard[3] = ''; gameOverBoard[6] = ''; winnerPlayer = tmpPlayer; } else{if ((gameBoard[1] == tmpPlayer) && (gameBoard[4] == tmpPlayer) && (gameBoard[7] == tmpPlayer)) { gameOverBoard[1] = ''; gameOverBoard[4] = ''; gameOverBoard[7] = ''; winnerPlayer = tmpPlayer; } else{if ((gameBoard[2] == tmpPlayer) && (gameBoard[5] == tmpPlayer) && (gameBoard[8] == tmpPlayer)) { gameOverBoard[2] = ''; gameOverBoard[5] = ''; gameOverBoard[8] = ''; winnerPlayer = tmpPlayer; } else{if ((gameBoard[0] == tmpPlayer) && (gameBoard[4] == tmpPlayer) && (gameBoard[8] == tmpPlayer)) { gameOverBoard[0] = '\\'; gameOverBoard[4] = '\\'; gameOverBoard[8] = '\\'; winnerPlayer = tmpPlayer; } else{if ((gameBoard[6] == tmpPlayer) && (gameBoard[4] == tmpPlayer) && (gameBoard[2] == tmpPlayer)) { gameOverBoard[6] = '/'; gameOverBoard[4] = '/'; gameOverBoard[2] = '/'; winnerPlayer = tmpPlayer; } } } } } } } } }
if (winnerPlayer == ' ') return 0; else return 1;}

Program Kalender


//////////////////////////////////////////////////////////////////// By Alfian ANsyar// STIMIK Balikpapan///////////////////////////////////////////////////////////////////
/* YOU MUST INDLUDE THE ABOVE BY COPYRIGHT LAW IF YOU CHANGE THIS CODE IN ANYWAY!*/
#include // Will set values for statements.int isleapyear(int x);void printWeekHeader();void printMonthHeader();void printdays(int m);int daystart; //globalint year; //globalvoid main(){ printf("Pilih tahun yang anda Inginkan?:");//Asks the user to input info. scanf("%d",&year);//Receives input. printf("\n\nMinggu =0\n"); printf("Senin =1\n"); printf("Selasa =2\n"); printf("Rabu =3\n"); printf("Kamis =4\n"); printf("Jum'at =5\n"); printf("Sabtu =6"); printf("\n\n hari pertama di bulan January?:"); scanf("%d",&daystart); getchar(); printMonthHeader();//Recalls the value for printMonthHeader. getchar();}
void printWeekHeader()//Assigns value to the statement printWeekHeader.{ printf("Mg Sn Sl Rb Km Jm Sa\n");//Print out letters.}
int isleapyear (int x)//Function evaluates if leap year or not.{
if (year==0)//Evaluates if the number given is equal to zero it will return { //a value of zero. Else the program will continue. return 0; } else { if (year%100!=0 && year%4==0)//Determines that if a year is divisible by { //400 it will return a value of 1. return 1; } else if (year%400==0)// If false it will return a value of 1. { return 1; } else//A value of zero is returned if we get this far. { return 0; }
}}
void printdays(int m)//Assigns value to the statement printdays.{ int count=0, looplimit,i;//Declares and initializes variables. int x; int s[12]={31,28,31,30,31,30,31,31,30,31,30,31};
if (m==1)//Sets a condition. If true it executes. { x=isleapyear(year); looplimit=s[m]+x;//Assings s[m]=x to looplimit. }
else//If the if statement is false this will be executed. { looplimit=s[m];//Assings s[m] to looplimit. }
for (i=0;i for (i=1;i<=looplimit;i++)//Condition will contunue to loop unill false. { printf("%2d ",i);//The results are printed two spaces reserved. count ++; if ( count==7)//If statement true 0 is assigned to count. { printf("\n"); count=0; } } daystart=count;//Daystart is set equal to count. printf("\n");}
void printMonthHeader()//Assigns value to the statement printMonthHeader.
{
printf("Januari\n");//Prints out statement. printWeekHeader();//Recalls value for printWeekHeader. printdays(0);//Recalls value for printdays. //Assigns the first value in the string to January etc. printf("\n"); getchar();//Program pauses after every month. printf("Februari\n"); printWeekHeader(); printdays(1); printf("\n"); getchar(); printf("Maret\n"); printWeekHeader(); printdays(2); printf("\n"); getchar(); printf("April\n"); printWeekHeader(); printdays(3); printf("\n"); getchar(); printf("Mei\n"); printWeekHeader(); printdays(4); printf("\n"); getchar(); printf("Juni\n"); printWeekHeader(); printdays(5); printf("\n"); getchar(); printf("Juli\n"); printWeekHeader(); printdays(6); printf("\n"); getchar(); printf("Agustus\n"); printWeekHeader(); printdays(7); printf("\n"); getchar(); printf("September\n"); printWeekHeader(); printdays(8); printf("\n"); getchar(); printf("Oktober\n"); printWeekHeader(); printdays(9); printf("\n"); getchar(); printf("November\n"); printWeekHeader(); printdays(10); printf("\n"); getchar(); printf("Desember\n"); printWeekHeader(); printdays(11); printf("\n");}//End.

semoga Program diatas bermanfaat.........

0 Comments:

Post a Comment



Newer Post Older Post Home