代码分享-判断当前控制台输入的ascii值

Author:dalerkd
0.2版本2015年3月25日 14:40:01

//判断当前控制台输入的ascii值,做进一步动作

#include "stdafx.h"
#include "stdlib.h"
#include "conio.h"
//本代码没有对“非数字”进行判断,这是为了程序的扩展。当然你可以通过对ascii的限制来完成判断的目的。
//本程序输入再多也不溢出哟
//是否可以通过外在的规则文件的输入使之能变通地解决一些问题?任Kitty—2015-3-11 20:20:27
//该函数总是可以获取当前输入的字符的ASCII,并通过switch-case对其进行判断.

int _tmain(int argc, _TCHAR* argv[])
{
    system("title 你好.按“T”键退出");

    while(1)
    {

        system("CLS");
        printf ("输入要判断的数:");
        char tmp[2];
        int i=0;

        while(1)
        {

            i=!(i);//一共有两个位置,通过这个语句使循环存储字符
            tmp[i]=_getch();
            putchar(tmp[i]);//回显输入,可关闭

            switch (tmp[i])//想对什么响应就加入case吧.
            {
                /*case '\r':
                {;
                }*/

            case 'T':
                {
                    exit(0);
                }
            case 't':
                {
                    exit(0);
                }


            default:
                break;
            }
        }
    }



}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java代码实现的五子棋游戏,棋盘使用ASCII字符画的形式,通过控制台输入棋子的坐标进行下棋,程序会自动判断胜负或平局: ```java import java.util.Scanner; public class FiveInARowGame { private static final int BOARD_SIZE = 15; // 棋盘大小 private static final char[][] board = new char[BOARD_SIZE][BOARD_SIZE]; // 棋盘数组 private static final char BLACK_PIECE = '●'; // 黑子 private static final char WHITE_PIECE = '○'; // 白子 private static final int[][] DIRECTIONS = new int[][]{ {1, 0}, // 右 {0, 1}, // 下 {1, 1}, // 右下 {1, -1} // 右上 }; public static void main(String[] args) { initBoard(); // 初始化棋盘 printBoard(); // 打印棋盘 Scanner scanner = new Scanner(System.in); char currentPiece = BLACK_PIECE; // 当前下棋的棋子,默认为黑子 while (true) { System.out.println("请 " + currentPiece + " 下棋,输入坐标(x,y):"); int x = scanner.nextInt(); int y = scanner.nextInt(); if (isValidMove(x, y)) { board[x][y] = currentPiece; // 下棋 printBoard(); // 打印棋盘 if (isWin(x, y, currentPiece)) { System.out.println(currentPiece + " 赢了!"); break; } if (isDraw()) { System.out.println("平局!"); break; } currentPiece = (currentPiece == BLACK_PIECE) ? WHITE_PIECE : BLACK_PIECE; // 切换下棋的棋子 } else { System.out.println("无效的坐标,请重新输入!"); } } } /** * 初始化棋盘 */ private static void initBoard() { for (int i = 0; i < BOARD_SIZE; i++) { for (int j = 0; j < BOARD_SIZE; j++) { board[i][j] = '┼'; // 初始为 '+' } } } /** * 打印棋盘 */ private static void printBoard() { System.out.print(" "); for (int i = 0; i < BOARD_SIZE; i++) { System.out.print((char) ('A' + i) + " "); } System.out.println(); for (int i = 0; i < BOARD_SIZE; i++) { System.out.printf("%2d ", i + 1); for (int j = 0; j < BOARD_SIZE; j++) { System.out.print(board[i][j] + " "); } System.out.println(); } } /** * 判断坐标是否有效 */ private static boolean isValidMove(int x, int y) { return x >= 0 && x < BOARD_SIZE && y >= 0 && y < BOARD_SIZE && board[x][y] == '┼'; } /** * 判断是否胜利 */ private static boolean isWin(int x, int y, char currentPiece) { for (int[] direction : DIRECTIONS) { int count = 1; // 统计相同颜色的棋子数量 int dx = direction[0], dy = direction[1]; // 方向 // 向左或向上延伸 for (int i = 1; i <= 4; i++) { int nx = x - i * dx, ny = y - i * dy; if (nx < 0 || ny < 0 || nx >= BOARD_SIZE || ny >= BOARD_SIZE || board[nx][ny] != currentPiece) { break; } count++; } // 向右或向下延伸 for (int i = 1; i <= 4; i++) { int nx = x + i * dx, ny = y + i * dy; if (nx < 0 || ny < 0 || nx >= BOARD_SIZE || ny >= BOARD_SIZE || board[nx][ny] != currentPiece) { break; } count++; } if (count >= 5) { return true; } } return false; } /** * 判断是否平局 */ private static boolean isDraw() { for (int i = 0; i < BOARD_SIZE; i++) { for (int j = 0; j < BOARD_SIZE; j++) { if (board[i][j] == '┼') { return false; } } } return true; } } ``` 使用控制台输入坐标进行下棋,例如输入 `10 10` 表示在第10行、第10列下棋。棋子颜色轮流为黑子和白子,程序会自动判断胜负或平局。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值